Simple array question...

M

main()

Hi all,

If 'a' is an array of ten characters , say

char a[10];

whenever i say 'a' in my code it means &a[0]. (except in sizeof
operator).
Then why is &a is same as &a[0] and not &(&a[0]) ?

Thanks for your time.
 
R

Richard Heathfield

main() said:
Hi all,

If 'a' is an array of ten characters , say

char a[10];

whenever i say 'a' in my code it means &a[0]. (except in sizeof
operator).

And except when it is the operand of the & operator, and except when you are
defining it - in fact, except any situation except the situation where you
are evaluating the array.
Then why is &a is same as &a[0] and not &(&a[0]) ?

But it isn't! &a has the type "pointer to array of char[10]", whereas &a[0]
has the type "pointer to char". Since they have different types, it's hard
to argue that they are "the same".

The thing to bear in mind is this: when you're using an array name in a
value context (e.g. a lookup, an assignment, whatever), the Standard
guarantees the following equivalence:

a == *(a + i)

Therefore, if we take addresses of either side:

&a == &*(a + i)

Now, & and * are inverses of each other, so they cancel on the RHS:

&a == (a + i)

Removing superfluous parentheses:

&a == a + i

Setting i to 0:

&a[0] == a + 0

Addition of 0 is redundant:

&a[0] == a

See? :)
 
J

Joe Wright

Richard said:
main() said:
Hi all,

If 'a' is an array of ten characters , say

char a[10];

whenever i say 'a' in my code it means &a[0]. (except in sizeof
operator).

And except when it is the operand of the & operator, and except when you are
defining it - in fact, except any situation except the situation where you
are evaluating the array.
Then why is &a is same as &a[0] and not &(&a[0]) ?

But it isn't! &a has the type "pointer to array of char[10]", whereas &a[0]
has the type "pointer to char". Since they have different types, it's hard
to argue that they are "the same".

The thing to bear in mind is this: when you're using an array name in a
value context (e.g. a lookup, an assignment, whatever), the Standard
guarantees the following equivalence:

a == *(a + i)

Therefore, if we take addresses of either side:

&a == &*(a + i)

Now, & and * are inverses of each other, so they cancel on the RHS:

&a == (a + i)

Removing superfluous parentheses:

&a == a + i

Setting i to 0:

&a[0] == a + 0

Addition of 0 is redundant:

&a[0] == a

See? :)

Priceless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top