Is &a[NTHINGS] legal
Theodore Stevens Norvell
norvell at utcsri.UUCP
Mon May 2 07:37:19 AEST 1988
In article <11289 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>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?
>> for (p = a; p < &a[NTHINGS]; p++) /* 1 */
>>Will 1 be guaranteed to work in ANSI-C?
>
>Yes. If necessary, the compiler will put a one-byte (or word or
>whatever) shim after the array in that array's address space, so
>that &a[NTHINGS] will be meaningful.
But, doesn't the draft (my copy is from November) also say that p+i
(where p is a pointer) can only be dereferenced if it points to the
same array as does p? Since &a[NTHINGS] translates to &(*(a+NTHINGS))
and a+NTHINGS does not point to the same array as does a, the dereference
is undefined. This allows array bounds checking in ANSI C.
Try for(p = a; p < a+NTHINGS; p++) /* 3 */
More information about the Comp.lang.c
mailing list