Pointer arithmetic on pointer of pointer (*argv[])?
I am aware that foo[bar]
is equal to *(foo + bar)
, but what is *foo[bar]
equal to, such as 开发者_开发问答accessing *argv[2]
? I am somewhat confused in understanding this, I assumed maybe something like *(*(foo) + bar)
but am unsure..
I apologize if this is a simple answer.
*a[b]
is equivalent to *(a[b])
due to C and C++ precedence rules. so
*a[b]
is equivalent to **(a+b)
If the following are equivalent,
foo[bar]
*(foo + bar)
Then the following are equivalent too:
*foo[bar]
**(foo + bar)
It's my understanding that it is **(foo + bar)
Why?
*foo[bar]
breaks down to * and foo[bar]
since * is done after foo[bar]
is dereferenced.
You already answered what foo[bar] == *(foo + bar)
Now add another * and you've got *(*(foo + bar))
Which also simplifies to **(foo + bar)
*foo[bar]
is the pointer dereference to foo[bar]
.
Assuming a declaration of char *argv[]
, argv[2]
refers to the third element of the argv
array, which is a char *
, and *argv[2]
dereferences this pointer, giving you the first character in that string.
精彩评论