Velocity Reviews > Simple array question...

# Simple array question...

main()
Guest
Posts: n/a

 08-07-2006
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]) ?

Richard Heathfield
Guest
Posts: n/a

 08-07-2006
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[i] == *(a + i)

Therefore, if we take addresses of either side:

&a[i] == &*(a + i)

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

&a[i] == (a + i)

Removing superfluous parentheses:

&a[i] == a + i

Setting i to 0:

&a[0] == a + 0

&a[0] == a

See?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Joe Wright
Guest
Posts: n/a

 08-07-2006
Richard Heathfield wrote:
> 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[i] == *(a + i)
>
> Therefore, if we take addresses of either side:
>
> &a[i] == &*(a + i)
>
> Now, & and * are inverses of each other, so they cancel on the RHS:
>
> &a[i] == (a + i)
>
> Removing superfluous parentheses:
>
> &a[i] == a + i
>
> Setting i to 0:
>
> &a[0] == a + 0
>
> Addition of 0 is redundant:
>
> &a[0] == a
>
> See?
>

Priceless.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---