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?