a[], *p: if 0 <= p - a < sizeof(a), must p point to an element of a?

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Jan 5 13:14:37 AEST 1990


In article <875 at stealth.acf.nyu.edu> brnstnd at stealth.acf.nyu.edu (Dan Bernstein) writes:
>Basically, I want to have a guaranteed test of whether p points to an
>element of a, with foo a[N] and foo *p. I know that *if* p points to an
>element of a, *then* 0 <= p - a < sizeof(a); is the reverse true? (If
>yes, please explain the logic.)

No, in fact no strictly conforming program could rely on the converse
since p-a would be an illegal operation when p didn't point into a[]
(or just past its last element).

>The standard should define pointer subtraction more carefully.

I think it was pretty darn careful.



More information about the Comp.std.c mailing list