Is &a[NTHINGS] legal

Chris Torek chris at mimsy.UUCP
Mon May 2 14:09:07 AEST 1988


>>In article <12074 at tut.cis.ohio-state.edu> lvc at tut.cis.ohio-state.edu
>>(Lawrence V. Cipriani) writes:
>>>	for (p = a; p < &a[NTHINGS]; p++)	/* legal? */

>In article <11289 at mimsy.UUCP> I answered:
>>Yes.

In article <5997 at utcsri.UUCP> norvell at utcsri.UUCP (Theodore Stevens Norvell)
writes:
>But, doesn't the draft ... also say that p+i (where p is a pointer)
>can only be dereferenced if it points to the same array as does p?

Yes.

>Since &a[NTHINGS] translates to &(*(a+NTHINGS))

No, not the way you mean this.  `Is equivalent to', not `evaluates as':
the indirection does not occur.  Only the address computation occurs;
the value at a+NTHINGS is not examined.

>... the dereference is undefined.

True.  It also does not occur.

>This allows array bounds checking in ANSI C.

Bounds checking is still possible, but &a[NTHINGS] must be allowed,
while both a[NTHINGS] and &a[NTHINGS+1] should be rejected.
-- 
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