Thread: Why aren't 2 chars equal?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Why aren't 2 chars equal?

    I'm new with c++ programming and i got this newb guestion.
    I got 2 variables, char a[5] = "hola"; and char b[5] = "hola";.
    The problem is that when i check them like this:
    Code:
    char a[5] = "hola";
    char b[5] = "hola";
    
    if(a == b) {
    cout << "Hello to you too\n";
    }
    else {
    cout << "No hello to you\n";
    }
    it says "No hello to you".
    So what i want to ask is why aren't those equal and how can i make variables a and b equal?

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You are not comparing two "chars" but actually you are comparing pointer to char arrays, which are two completely different things. Since you are not using the std::string type (that supports comparison using the == operation), you have to understand how C strings work.

    In fact, a and b are pointers to the first element of each respective array. Imagine that they do not contain a specific character but rather an address to the location where the first character of the array is located. If the address of a is "0000" and the address of b is "1000", then when you write a==b, you effectively compare the two addresses, which, as you can see, are different.

    If you want to compare two C strings, use the strcmp() function that does the job for you:

    Code:
    char a[5] = "hola";
    char b[5] = "hola";
    
    if (strcmp(a,b) == 0)
    {
    cout << "Hello to you too\n";
    }
    else
    {
    cout << "No hello to you\n";
    }
    Since you are programming in C++, you could just write:
    Code:
    std::string a, b;
    a = "hola";
    b = "hola";
    if(a == b) {
    cout << "Hello to you too\n";
    }
    else {
    cout << "No hello to you\n";
    }
    since then you're using the overloaded comparison operator of the string object.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's a good job you didn't try

    char *a = "hola";
    char *b = "hola";
    if(a == b)

    Which might be the right answer for entirely the wrong reason
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Thanks, that made alot sense.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are new to C++ you should not be using character arrays to store strings. Use the C++ string class instead.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    It's a good job you didn't try

    char *a = "hola";
    char *b = "hola";
    if(a == b)

    Which might be the right answer for entirely the wrong reason
    Sometimes I use a string caching layer which inserts strings into a hash table and returns a pointer to the instance of the string in the hash. If you do this consistently, you CAN compare strings for equality by just checking if the pointers are equal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wierd chars...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2006, 07:18 PM
  2. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM
  3. chars not equal
    By krappykoder in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2002, 08:05 AM
  4. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  5. how can 0.00665 not equal 0.00665??!!
    By Susan in forum C++ Programming
    Replies: 10
    Last Post: 02-10-2002, 02:33 AM