Is &a[NTHINGS] legal

Jim Patterson jimp at cognos.uucp
Wed May 11 23:16:25 AEST 1988


In article <52339 at sun.uucp> limes at sun.UUCP (Greg Limes) writes:
>In article <1086 at gazette.bcm.tmc.edu> biff at eyeball.ophth.bcm.tmc.edu.UUCP (Brad Daniels) writes:
>>
>>Whether or not the above is legal, I think it should probably be written as:
>>
>>	for (p = a; p < (a+NTHINGS); p++)
>>
>>Or am I missing something obvious?
>
>What if (a+NTHINGS) wraps around the address space in such a way that
>(a+NTHINGS) < a?

The ANSI C draft explicitly allows for the syntax shown above (where a is
declared "a[NTHINGS]". This is found in section 3.3.6 "Additive Operators"
of the draft. The last sentence reads:

    "However, if P points to the last member of an array object, the
    expression (P+1) - P has the value 1, even though P+1 does not 
    point to a member of the array object".

(ANSI Doc X3J11/88-002 page 48). 

This is further clarified in the Rationale document. However, the
intention is that such expressions should work as expected. In
practice, the effect is that generally at least one addressable byte
needs to follow any declared array such that its address follows that
of the last array element.

So, according to the ANSI committee, &a[NTHINGS] IS legal.
-- 
Jim Patterson                              Cognos Incorporated
UUCP:decvax!utzoo!dciem!nrcaer!cognos!jimp P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
                                           Ottawa, Ont  K1G 3Z4



More information about the Comp.lang.c mailing list