: I think you've found an error in K&R2. (BTW, it's "The C Programming
: Language", not C++.) It's not mentioned in the errata at
: <
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html> either.
I think you found an error in my article ;-)
Of course it should be "The C Programming Language".
: Section A7.9, "Relational Operators", discusses the "<", ">", "<=",
: and ">=" operators. It says, in part:
: Pointers comparison is defined only for parts of the same object:
: [...] Otherwise, pointer comparison is undefined.
: Section A7.10, "Equality Operators", discusses the "==" and "!="
: operators. It says, in part:
: The equality operators follow the same rules as the relational
: operators, but permit additional possibilities: a pointer may be
: compared to a constant integral expression with value 0, or to a
: pointer to void. See section A6.6.
: (Section A6.6 doesn't address the issue of comparing pointers to
: distinct objects for equality.)
: The corresponding sections in the C90 and C99 standard say that
: relational operators on pointers to distinct objects cause undefined
: behavior (except perhaps in some obscure circumstances involving
: objects that happen to be adjacent in memory), but doesn't make a
: similar statement about equality operators, implying that equality
: comparison of pointers to distinct objects is well-defined (and yields
: the value 0).
: I hesitate to suggest that the creators of the language got something
: like this wrong; it can happen, but I'm hardly immune to making
: mistakes myself. I encourage other readers to check my interpretation
: and point out whether I've missed something or K&R have.
I also found it hard to believe that equality comparison on valid
pointers to distinct objects would be UB, but K&R seemed to lead to
no other conclusion. That's why I asked the group.
Thank you for your response, anyway.
: --
: Keith Thompson (The_Other_Keith) (e-mail address removed) <
http://www.ghoti.net/~kst>
: San Diego Supercomputer Center <*> <
http://users.sdsc.edu/~kst>
: Schroedinger does Shakespeare: "To be *and* not to be"