[net.lang.c] arrays/pointers in C

jas@rtech.UUCP (Jim Shankland) (06/02/85)

> 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

bright@dataio.UUCP (Walter Bright) (06/06/85)

In article <459@rtech.UUCP> jas@rtech.UUCP (Jim Shankland) writes:
>An identifier of an array object is ALWAYS read as a pointer to the
>first element of the array, except in the declaration itself.

Not quite true. Inside of a sizeof expression, arrays are not converted
to pointers. For example,

	int a[4][5];

	b = sizeof(a[2]);

b is assigned 5*sizeof(int), not sizeof(int *). Another C feature that
can only be discerned by reading between the lines.

darryl@ISM780.UUCP (06/11/85)

>                                                Another C feature that
>can only be discerned by reading between the lines.

Pardon me, but the lines you are looking for are at the top of page 188
of K&R, and they read:

    "The sizeof operator yields the size, in bytes, of its operand.
    (A byte is undefined by the language except in terms of the value of
    sizeof.  However, in all existing implementations a byte is the space
    required to hold a char.)
			       When applied to an array, the result is the
    total number of bytes in the array.

Actually, most (but certainly not all!) C features can be determined
by reading the lines.

	    --Darryl Richman, INTERACTIVE Systems Corp.
	    ...!cca!ima!ism780!darryl
	    The views expressed above are my opinions only.