Thread: What function to use to check the equality of two strings?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    What function to use to check the equality of two strings?

    i wanted to compare two strings in a conditional. but
    Code:
     if (string1==string2)
    wont' work.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    or _stricmp() for a lower-case comparison.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Bubba View Post
    or _stricmp() for a lower-case comparison.
    As long as your compiler supports that function.

    strncmp() is another standard function you can use if you only want to compare part of a string.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    stricmp variant is for case insensitivity. Not "lowercase".

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    if (string1==string2)
    Just so you're aware of what's going, you're comparing pointers. So instead of comparing the array like you want to, you're comparing two variables that contain memory addresses. So unless the two pointers both point to the exact same location in memory, they're not equal - even if they point to the same data.

    strcmp compares the arrays element by element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM