Printing CLA using pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evantri
    New Member
    • Sep 2006
    • 25

    Printing CLA using pointer

    Write a C program that will use a char ** pointer to print out each command line argument.

    I know that I should use for loop, but I have no idea how to initiate and terminate the loop.

    Could someone help me please? Thanx
  • dtimes6
    New Member
    • Oct 2006
    • 73

    #2
    Originally posted by evantri
    Write a C program that will use a char ** pointer to print out each command line argument.

    I know that I should use for loop, but I have no idea how to initiate and terminate the loop.

    Could someone help me please? Thanx
    main( int argc, char** argv)

    * argc is the count of the args.
    * argv is the input from the command line

    If ur application is Test.exe
    u exec it like this: Test.exe -a -b Test.file
    argc == 4
    argv[0] == "Test.exe"
    argv[1] == "-a"
    argv[2] == "-b"
    argv[3] == "Test.file"

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      in main

      int main(int argc, char **argp)
      {
      }

      argc holds the number of pointers in the array argp that contain data. Use an integer variable starting at 0 and with a stop condition of <argc in your for loop.

      Then access argp[LoopVariable] to get the argument values.

      Comment

      • evantri
        New Member
        • Sep 2006
        • 25

        #4
        Originally posted by Banfa
        in main

        int main(int argc, char **argp)
        {
        }

        argc holds the number of pointers in the array argp that contain data. Use an integer variable starting at 0 and with a stop condition of <argc in your for loop.

        Then access argp[LoopVariable] to get the argument values.
        I know about the meaning of argv (the command line input) and argc (argument count). so far i can write the code until

        Code:
        int main ( int argc, char ** argv )
        
        {
        
        int i;
        
        char ** print_argv;
        
        for ( print_argv = ...; print_argv != NULL; print_argv ++ )
                    {
                    // print the argv here
                    }
        
        }
        Am I goin in the right track? Do I have to two FOR loops instead of one FOR loop? How do I initiate the loop? Thanx

        Comment

        • evantri
          New Member
          • Sep 2006
          • 25

          #5
          Anyway, what's wrong with my code here?

          Code:
          int main ( int argc, char ** argv )
          
          {
          
          // Mainline Variable Declarations
          FILE * output = stdout;
          
          int i; // Loop index
          
          char ** print_argv; // Pointer of pointer to character ( array of strings )
          
          fprintf ( output, "\n" );
          
          for ( print_argv = argv[0], i = 0; print_argv != NULL; print_argv ++, i ++ )
                  {
                  fprintf ( output, "argv[%d] is %s\n", i, print_argv );
                  }
          
          fprintf ( output, "\n" );
          
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Declare

            char ** print_argv;

            as

            char * print_argv;

            Comment

            • evantri
              New Member
              • Sep 2006
              • 25

              #7
              Originally posted by Banfa
              Declare

              char ** print_argv;

              as

              char * print_argv;
              But the specification requires that the pointer that I suppose to use is a type of ** pointer? So how do I modify the FOR loop using a char ** pointer?

              Comment

              Working...