By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Printing CLA using pointer

P: 25
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
Nov 2 '06 #1
Share this Question
Share on Google+
6 Replies


P: 73
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"
Nov 2 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 2 '06 #3

P: 25
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

Expand|Select|Wrap|Line Numbers
  1. int main ( int argc, char ** argv )
  2.  
  3. {
  4.  
  5. int i;
  6.  
  7. char ** print_argv;
  8.  
  9. for ( print_argv = ...; print_argv != NULL; print_argv ++ )
  10.             {
  11.             // print the argv here
  12.             }
  13.  
  14. }
  15.  
  16.  
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
Nov 2 '06 #4

P: 25
Anyway, what's wrong with my code here?

Expand|Select|Wrap|Line Numbers
  1. int main ( int argc, char ** argv )
  2.  
  3. {
  4.  
  5. // Mainline Variable Declarations
  6. FILE * output = stdout;
  7.  
  8. int i; // Loop index
  9.  
  10. char ** print_argv; // Pointer of pointer to character ( array of strings )
  11.  
  12. fprintf ( output, "\n" );
  13.  
  14. for ( print_argv = argv[0], i = 0; print_argv != NULL; print_argv ++, i ++ )
  15.         {
  16.         fprintf ( output, "argv[%d] is %s\n", i, print_argv );
  17.         }
  18.  
  19. fprintf ( output, "\n" );
  20.  
  21. }
  22.  
Nov 2 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Declare

char ** print_argv;

as

char * print_argv;
Nov 2 '06 #6

P: 25
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?
Nov 2 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.