pointer comparison question
Tim Olson
tim at crackle.amd.com
Tue Nov 15 02:38:10 AEST 1988
In article <185 at umigw.MIAMI.EDU> steve at umigw.miami.edu (steve emmerson) writes:
| The question is this: is the following expression guaranteed to return
| true if and only if "TestPtr" points within the array:
|
| static struct foo array[NUM];
| static struct foo BegPtr = array[0];
| static struct foo BegPtr = array[NUM-1];
| extern struct foo *TestPtr; /* could also come from a
| function call */
|
| TestPtr >= BegPtr && TestPtr <= EndPtr; /* guaranteed to do what
| I want? */
|
| Or is it "implementation defined" or "undefined". Note the "if and only
| if" condition, which means that if the expression is false, then "TestPtr"
| *must* point "outside" the array.
It is undefined. The "iff" condition also means that if the expression
is true, then "TestPtr" must point *inside* the array, which is the
problem here.
Section 3.3.8 of the ANSI Draft says:
"... If the objects pointed to are not members of the same
aggregate or union object, the result is undefined."
-- Tim Olson
Advanced Micro Devices
(tim at crackle.amd.com)
More information about the Comp.lang.c
mailing list