Array bounds checking: what is legal

Brian Matthews 6sigma2 at polari.UUCP
Tue Sep 4 09:16:49 AEST 1990


In article <26327 at mimsy.umd.edu> chris at mimsy.umd.edu (Chris Torek) writes:
|Correct.  Given `int a[4];', the following holds:
|	int *p = a;			/* legal */
|	a[0], a[1], a[2], a[3];		/* all legal */
|	p[0], p[1], p[2], p[3];		/* all legal */
|	p = &a[4];			/* legal */
|	*p;				/* illegal (a[4] does not exist) */
|	p--;				/* legal */
|	p = a;				/* legal */
|	p--;				/* illegal */
|	p = &a[4];			/* legal */
|	p[-4], p[-3], p[-2], p[-1];	/* all legal */

A minor clarification - where Chris says "illegal", read "undefined".
Admittedly a minor point, but "illegal" might lead one to believe the
compiler won't accept the code in question.  "Undefined" means the
compiler may or may not accept the code, and if accepted, the resulting
machine code may or may not do something "useful", which is actually
the case.
-- 
Brian L. Matthews	blm at 6sceng.UUCP



More information about the Comp.lang.c mailing list