Is &a[NTHINGS] legal
Chris Torek
chris at mimsy.UUCP
Sun May 1 02:09:21 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?
Depends:
>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 */
This is not necessary.
>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.
Note, however, that the corresponding count-down loop
for (p = &a[NTHINGS]; --p >= a;)
is not. In particular, this sort of code fails if &a[-1] `wraps
around' the address space of the array. It can even fail on a flat
address space machine like the Vax if sizeof(a[0]) is large enough.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list