Is &a[NTHINGS] legal

Doug Gwyn gwyn at brl-smoke.ARPA
Sun May 1 02:06:59 AEST 1988


In article <12074 at tut.cis.ohio-state.edu> lvc at tut.cis.ohio-state.edu (Lawrence V. Cipriani) writes:
>Is it legal to apply the & (address of) operator to an array
>element that is non-existent?  Given:
>	sometype a[NTHINGS], *p;
>Should:
>	for (p = a; p < &a[NTHINGS]; p++)	/* 1 */
>be written as:
>	for (p = a; p <= &a[NTHINGS-1]; p++)	/* 2 */
>		...
>Will 1 be guaranteed to work in ANSI-C?

Yes, it is.  This kind of code is quite pervasive, and if you
consider that NTHINGS might have been defined as 0 it is impossible
to avoid (in fact in that situation your case 2 is invalid).

Every object must have at least one addressable cell beyond it,
but not necessarily in front of it.  The reason the latter is not
required is that &a[-1] may be MANY bytes in front of allocated
storage if the array element is large, but &a[NTHINGS] will be
just one byte past the valid array locations.



More information about the Comp.lang.c mailing list