Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!ames!oliveb!sun!gorodish!guy From: guy@gorodish.UUCP Newsgroups: comp.lang.c Subject: Re: Pointer Comparison and Portability Message-ID: <13357@sun.uucp> Date: Sun, 15-Feb-87 19:20:06 EST Article-I.D.: sun.13357 Posted: Sun Feb 15 19:20:06 1987 Date-Received: Mon, 16-Feb-87 04:04:31 EST References: <416@vaxine.UUCP> <454@mipos3.UUCP> <636@sdchema.sdchem.UUCP> Sender: news@sun.uucp Reply-To: guy@sun.UUCP (Guy Harris) Organization: Sun Microsystems, Mountain View Lines: 48 >In article <454@mipos3.UUCP> pinkas@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@sdchema.sdchem.UUCP> tps@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.