pointers to arrays

Guy Harris guy at auspex.UUCP
Tue Feb 21 04:57:49 AEST 1989


>E is actually an address itself; it is the address
>of the first element of the array.

"E" is an array.  It is, in *most* contexts, converted to a pointer to
the first element of the array.  In dpANS C (probably pANS C as well),
"operand of 'sizeof'" and 'operand of unary &' are not among those
contexts.

>This is in K&R, I believe.

In K&R Second Edition it says what the previous paragraph says; see A7.1
Pointer Generation.

K&R First Edition says:

	An identifier is a primary expression, provided it has been
	suitably declared as discussed below.  Its type is specified by
	its declaration.  If the type of the identifier is "array of
	...", however, then the value of the identifier-expression is a
	pointer to the first object in the array, and the type of the
	expression is "pointer to ..."

	...

	   The 'sizeof' operator yields the size, in bytes, of its
	operand.  ...  When applied to an array, the result is the
	total number of bytes in the array.

This had some errors:

	1) K&R First Edition C allows you to declare something of type
	   "pointer to array ...", but the stuff in the first paragraph
	   speaks only of identifiers of type "array", not of arbitrary
	   array-valued expressions.

	   The (d)pANS fixes that, by indicating that the "array to
	   pointer to first element" conversion applies to all
	   "array-valued expressions".

	2) The stuff in the second paragraph actually renders the stuff
	   in the first paragraph not 100% correct, since in:

		char foo[73];

		return sizeof foo;

	    "foo" is *not*, in fact, a pointer to the first element of
	    "foo"; if it were, "sizeof foo" would yield the size of a
	    pointer to "char", not the size of a 73-element array of
	    "char".

	    The (d)pANS fixes that by indicating that the conversion in
	    question does not apply to the operand of the 'sizeof'
	    operator.

The (d)pANS also states that the conversion in question does not apply
to the operand of the unary & operator; this is an extension that breaks
no valid code (since, previously, you weren't supposed to apply "&" to
an array, since leaving the "&" out yielded the same result), and cleans
the language up somewhat.



More information about the Comp.lang.c mailing list