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

brnstnd at stealth.acf.nyu.edu brnstnd at stealth.acf.nyu.edu
Wed Jan 10 11:59:50 AEST 1990


The answer to the specific question in the subject: No. The standard
guarantees nothing; and the test will fail on micros in practice. It may
also fail on some real computers if the type is of size 2 or more and if
p points between elements of a.

The answer to the question of how to reliably test whether p points to
an element of a: Run through the elements of a and test each against p.
There doesn't seem to be a faster portable method.

When ANSI says ``undefined,'' it really does mean that your computer
could blow up. This is an unfortunate gap in the standard (though, I
admit, not a sufficient incentive to switch to Ada).

---Dan



More information about the Comp.std.c mailing list