"array" vs. "&array" ?

Karl Heuer karl at haddock.ima.isc.com
Sat Jan 13 06:46:07 AEST 1990


In article <5248 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
>Usually when you ask for the pointer to an array you get a
>pointer to the first element of the array.

No, you get a pointer to the entire array (in ANSI C) or an error (in K&R C),
except in those pre-ANSI compilers that chose, as an extension, to make the
misinterpretation you describe (usually accompanied by a warning).

>E.g.:
>	type_t *aptr, a[MAXINDEX+1];
>	aptr = &a;

I don't know of any compiler that will accept that line without a diagnostic.

To reiterate the ANSI convention (the only one worth mentioning, since the
other behavior is really just correcting a "probably typo" for &a[0]):
	type_t (*aptr)[SIZE], a[SIZE];
	aptr = &a;
	if ( *aptr == a )
	    puts( "Will always print this." );

Note that "*aptr" and "a" are both array objects, and hence in the rvalue
context of the compare *each* will be converted to a pointer:
	if ( &(*aptr)[0] == &a[0] )

Karl W. Z. Heuer (karl at haddock.isc.com or ima!haddock!karl), The Walking Lint



More information about the Comp.lang.c mailing list