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

T. William Wells bill at twwells.com
Fri Jan 5 15:07:10 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 general, if p does not point to a member of a, p - a is
undefined.

: The standard should define pointer subtraction more carefully.

It has defined this most explicitly. Since my fingers are tired,
I won't type in the appropriate text. But it is in section 3.3.6.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com



More information about the Comp.std.c mailing list