2

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.

1
  • 1
    You could simply try it out. When in doubt, use parentheses. Commented Nov 25, 2010 at 17:32

5 Answers 5

5

*a[b] is equivalent to *(a[b]) due to C and C++ precedence rules. so *a[b] is equivalent to **(a+b)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! that is much simpler to understand. I knew it was simple, just need to wait 11 minutes to accept :P
3

If the following are equivalent,

foo[bar]
*(foo + bar)

Then the following are equivalent too:

*foo[bar]
**(foo + bar)

3 Comments

Do I understand correctly that your if part is meant to deal with the fact that * and [] can be overloaded?
@Armen Tsirunyan: I think the "If...then" is just demonstrating the process of logical deduction that can be followed to find the result.
@Armen: Right; what @caf said. Thanks for reading my mind, @caf.
2

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)

Comments

0

*foo[bar] is the pointer dereference to foo[bar].

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.