Comparing Pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sebastien.fricker@gmail.com

    #1

    Comparing Pointer

    Does anybody know if it is allowed to compare pointer using the
    operators '<', '<=', '>' or '>='?
  • Richard Bos

    #2
    Re: Comparing Pointer

    sebastien.frick er@gmail.com wrote:
    Does anybody know if it is allowed to compare pointer using the
    operators '<', '<=', '>' or '>='?
    Yes, but with limitations. You're allowed to compare any two valid or
    null pointers of compatible types[1] using == and !=. Using <, >, <= and
    >=, though, there is the additional demand that they must point within
    the same object (array, struct, union, etc.). If not, the behaviour is
    undefined. That also means that you can't compare to a null pointer
    (constant) using < or >.

    Richard

    [1] Or pointers to void, or pointers to qualified versions of compatible
    types, or null pointer constants.

    Comment

    • Keith Thompson

      #3
      Re: Comparing Pointer

      sebastien.frick er@gmail.com writes:
      Does anybody know if it is allowed to compare pointer using the
      operators '<', '<=', '>' or '>='?
      If your textbook doesn't answer that question, get a better one. I
      recommend K&R2 (Kernighan & Ritchie, _The C Programming Language_, 2nd
      Edition).

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • Ike Naar

        #4
        Re: Comparing Pointer

        In article <ln3b9oowod.fsf @nuthaus.mib.or g>,
        Keith Thompson <kst-u@mib.orgwrote:
        >sebastien.fric ker@gmail.com writes:
        >Does anybody know if it is allowed to compare pointer using the
        >operators '<', '<=', '>' or '>='?
        >
        >If your textbook doesn't answer that question, get a better one. I
        >recommend K&R2 (Kernighan & Ritchie, _The C Programming Language_, 2nd
        >Edition).
        In some prints of the book, the section on pointer comparisons
        (A7.10) has an omission that is corrected in the errata, see
        http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html .

        Comment

        Working...