arrays/pointers in C

Jim Shankland jas at rtech.UUCP
Sun Jun 2 15:40:54 AEST 1985


> array==pointer ... is only true when "array" is being passed
> or received as a parameter.  In this case, the C [sic] automatically
> changes all array names (e.g., foo(arr)) into addresses of the
> first element (e.g., foo(&arr[0])).

At the risk of picking nits, the above statement is not quite correct:

An identifier of an array object is ALWAYS read as a pointer to the
first element of the array, except in the declaration itself.  The subscript
operator ("[]") is a variant of the dereferencing operator '*' that supports
the notion of an offset.  "a[b]" is completely, syntactically, semantically,
and rigorously IDENTICAL to "*(a+b)" (except, again, in an actual declaration).
Of course, for the above to make sense, one of the operands must be a
pointer to something, and the other an integral type; but it doesn't matter
which is which.  It follows that "a[b]" is equivalent to "b[a]" -- a
notion that most people will not accept without a little head-scratching.

So "&arr[0]" is equivalent to "arr", since "&arr[0]" is identical to
"&*(arr+0)".  Far from internally converting "foo(arr)" into "foo(&arr[0]"),
the prudent compiler would read the latter construct, fold out the addition
with 0 and the "&*", and be left with the former construct.

Jim Shankland
..!ucbvax!mtxinu!rtech!jas
..!ihnp4!pegasus!rtech!jas



More information about the Comp.lang.c mailing list