Pointers and Arrays
karl at haddock
karl at haddock
Thu Aug 14 03:03:00 AEST 1986
hadron!jsdy writes:
>I have seen several references to the address of an array vs.
>the address of the first element of the array. Would someone
>care to address what they think this difference is, aside from
>data type?
On most machines, as you imply, &a and &a[0] do indeed have the same
bit-pattern, and will compare equal if you cast them to a common type.
If, however, you want to *do* something with the pointer (*, [], +, -,
++, --, etc.) you'd better have the correct type as well as value. In
particular, the effect of ++p on an int(*)[] is not the same as on an int*.
Btw, someone suggested earlier that ANSI C doesn't interpret &a as pointer
to array. I think it does: "The operand of the unary & operator shall be
a function locator or an lvalue [other than bit-field or register]. ...
The result ... is a pointer to the object [and has type] `pointer to _t_y_p_e'."
(3.3.3.2, 01-May-1986 draft.) Arrays are not mentioned as a special case.
And yes, arrays *are* (non-modifiable) lvalues in X3J11.
Karl W. Z. Heuer (ihnp4!ima!haddock!karl), The Walking Lint
More information about the Comp.lang.c
mailing list