Thread: Comparing pointers

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    Comparing pointers

    Hey,
    Basically, I have 3 pointers, all of which were returned from different strchr calls on the same string, with different search characters.
    So, from these 3 pointers I want to find which one of these characters occurs first
    My method of doing this is something like
    Code:
    if(((p1 < p2) || (p2 == NULL)) && ((p1 < p3) || (p3 == NULL))
    To check if p1 is first, taking into account p2 and p3 might be NULL.

    My question is this: Is the above snippet of code 'correct'? As in, is it safe to assume for any given string, and any 3 pointers to characters in that string retrieved using strchr, on any modern machine, the above would work as expected?
    I'm fairly confident that it's logically sound, but I'd greatly appreciate input on this matter.

    Thanks in advanced.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That should do what you want, except if p1 is NULL (in which case it will also probably be true, if NULL is treated as 0).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yup.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Well, I'm convinced.
    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 01:24 AM