Re: Understanding char **argv
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Sat, 05 Jul 2008 18:10:33 -0400
kevin.eugene08@xxxxxxxxxxxxxx wrote:
Hello all,
Forgive the somewhat simple question I am sure this will be, but I am
having some problem understanding what: char **argv looks like. For
instance, i know through trial and error that if I do this:
#include <stdio.h>
int main(int argc, char *argv[])
{
int l;
for( l=0; l<argc; l++ )
{
printf("Pointer: %p\tValue: %s\n", argv++, *argv);
}
return 0;
}
And i invoke the above as:
./foo one two three
Then I will see:
0x10202 foo
0x10204 one
0x10208 two
ox1020c three
Listed, but I would have expected to have needed to write:
*(*(argv))
So as to dereference what the pointer that *argv pointed to. Or have
I missed the point?
Maybe two points.
1. The parens are not needed. 'char **argv' is what it is, a pointer to a pointer to char.
2. In your printf statement above you have both argv++ and *argv. Because we have no way to know which expression might be evaluated first, this is classic Undefined Behavior.
#include <stdio.h>
int main(int argc, char **argv)
{
int l;
for (l = 0; l < argc; l++) {
printf("Pointer: %p\tValue: %s\n", argv, *argv);
argv++;
}
return 0;
}
...is the way I would do it.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.
- References:
- Understanding char **argv
- From: kevin.eugene08@xxxxxxxxxxxxxx
- Understanding char **argv
- Prev by Date: Re: Understanding char **argv
- Next by Date: Re: Understanding char **argv
- Previous by thread: Re: Understanding char **argv
- Next by thread: Re: Understanding char **argv
- Index(es):
Relevant Pages
|