Pointer Comparison and Portability

guy at gorodish.UUCP guy at gorodish.UUCP
Mon Feb 16 10:20:06 AEST 1987


>In article <454 at mipos3.UUCP> pinkas at mipos3.UUCP (Israel Pinkas) writes:
>>Considering that K&R specically said that only pointers to the same array
>>should be compared,

To be precise, they said

	Pointer comparison is portable only when the pointers point
	to objects in the same array.

when discussing "<", ">", "<=", and ">=".  Under "Equality
operators", they say that they're "exactly analogous" to relational
operators.  It is unclear whether this was intended to mean that
*all* pointer comparisons are portable only when the pointers point
to objects in the same array, or just comparisons other than for
equality or inequality.  I would vote for the latter, since pointer
*equality* can be defined as meaning "the two pointers point to the
same object.

In fact, ANSI X3J11 has already voted for the latter.

Under "Relational operators", they say that "If the objects pointed
to are not members of the same array, the result is undefined."
(Note, however that they also point out that "If P points to the last
member of an array object, the pointer expression P+1 is greater than
P, even though P+1 does not point to a member of the same array
object as P," which means that if P can be the last address in a
segment, you can't just naively compare offsets within a segment.)

Under "Equality operators", however, they say that "If two pointers
to objects or functions compare equal, they point to the same object
or function, respectively."

In article <636 at sdchema.sdchem.UUCP> tps at sdchemf.UUCP (Tom Stockfisch) writes:
>I would say that a function call which compared its two pointer arguments
>would *have* to work, no matter how K&R are interpreted on this point, or
>else strcpy wouldn't work.
>
>That is, even on a segmented architecture, when pointers get passed to a
>subroutine, they MUST have distinct addresses -- how else can the subroutine
>know what area of memory to access?

This isn't necessarily the case.  If one takes the first of the two
interpretations of K&R, two pointers that pointed to different
locations would compare equal.  This would be highly undesirable
(since people generally expect that two pointers will be equal iff
they point to the same object, which is presumably why ANSI ruled out
that interpretation), but would conform to the first interpretation
of K&R.



More information about the Comp.lang.c mailing list