arnuld wrote:
>> On Thu, 16 Aug 2007 12:58:31 +0000, Erik Wikström wrote:
>
>> I usually write this as
>>
>> int main (int argc, char* argv[])
>>
>> That is, argv is an array of pointers to char, or argv is an array of
>> pointers to NULL-terminated C-strings, where each string is an
>> argument passed to the program, and argc is the number of elements
>> in the array. In fact argc is short for "argument count" and argv is
>> "argument values".
>>
>> This reduces the problem to a loop over an array and printing
>> C-strings.
>
> ok, here it is in 2 versions, one with indexing and one with pointers,
> both fall under an infinite-loop :(
>
>
> #include <iostream>
>
> int main( int argc, char *argv[] )
> {
> std::cout << "These arguments were passed to 'main()'" << std::endl;
>
>
> /* an int value can be printed easily */ std::cout << argc <<
> std::endl;
>
> int i = 0;
>
> while( i < argc )
> {
> std::cout << argv[i] << "\n";
Are you hoping the compiler will increment 'i' for you?
> }
>
> return 0;
> }
>
>
> ------- pointer version ------------------- #include <iostream>
>
> int main( int argc, char *argv[] )
> {
> std::cout << "These arguments were passed to 'main()'" << std::endl;
>
>
> /* an int value can be printed easily */
> std::cout << argc << std::endl;
>
> char *pchar = argv[0];
>
> while( *pchar != '\0' )
> {
> std::cout << *pchar << "\n";
Even if you increment 'pchar' here (which is needed, of course,
because the compiler cannot figure out what you want it to do
unless you actually tell it), you will just print out all the
characters of the first argument to your program, one character
on each line. All other arguments will be left out.
> }
>
> return 0;
> }
Here is the solution with pointers:
while (*argv)
std::cout << *argv++ << std::endl;
And if you need to keep the value of 'argv' intact, copy it into
another pointer to pointer to char:
char **pstr = argv;
while (*pstr)
std::cout << *pstr++ << std::endl;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask