Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Is &a[NTHINGS] legal Message-ID: <11289@mimsy.UUCP> Date: 30 Apr 88 16:09:21 GMT References: <12074@tut.cis.ohio-state.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 31 In article <12074@tut.cis.ohio-state.edu> lvc@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@mimsy.umd.edu Path: uunet!mimsy!chris