+ Reply to Thread
Results 1 to 2 of 2

Thread: Can you compare two strings like string1==string2?

  1. #1
    JavaGuy Guest

    Can you compare two strings like string1==string2?

    Can you compare two strings like string1==string2? Why do we need strcmp()?

  2. #2
    JavaGuy Guest
    Do you think this will work?
    Code:
    if(string1 == string2)
    {
    
    }
    No!, strings in C cannot be compared like that!.

    The == operator will end up comparing two pointers (that is, if they have the same
    address). It wont compare the contents of those locations. In C, strings are represented
    as arrays of characters, and the language never manipulates (assigns, compares, etc.)
    arrays as a whole.

    The correct way to compare strings is to use strcmp()

    Code:
    if(strcmp(string1, string2) == 0) 
    {
    
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Compare two Binary Trees
    By TopGun in forum Adobe
    Replies: 1
    Last Post: 18th July 2008, 13:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts