String recognition problems? (Basic)

Discussion in 'Mac Programming' started by SAEinSDSU, Feb 17, 2010.

  1. SAEinSDSU macrumors newbie

    Feb 17, 2010
    Okay this is my first post on these forums with a very basic problem i have run into. I cannot seem to understand why my strings will not recognize each other as being the same. if i have code

    char a[] = "test";
    char b[] = "test";
    if(a == b){
    printf("%S", a);
    return (0);

    it will not print out a. Is this because the compiler cannot recognize these strings as equal by address or am i just writing the code wrong? Any answers would be great! thanks
  2. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    Although you haven't specified the language I will assume by your stated results that it is 'C'. If true then the use of '==' is comparing the addrresses of each string 'a' and 'b' and determining that they are not the same since their addresses are different.

    You need to look into the standard 'C' library function 'strcmp'.
  3. Detrius macrumors 68000

    Sep 10, 2008
    Asheville, NC
    or another way of saying it:

    You have a blue ball in your left hand and a blue ball in your right hand. Your comparison checks to see if the two balls are in the same hand--not whether the balls themselves are the same.

    A string is just an array of characters, and in C, if you reference an array without an index, you're just referencing the pointer value--the address of the first element in the array. Instead, you need to step through the arrays and compare each element, roughly like this:

    bool are_equal = true;
    int x=0;
    while( ( a[x] != '\0' ) && ( b[x] != '\0' ) )
        if ( a[x] != b[x] )
            are_equal = false;
    Technically, this code doesn't take into account that one string could be longer than the other, but you get the idea.

    As stated already, strcmp() does this for you--without the bug. ;)
  4. SAEinSDSU thread starter macrumors newbie

    Feb 17, 2010
    Okay so yes I understand the address confusion situation and follow your explanation of another way of doing it det...thank you!

    Would it be possible to compare the two strings by address in this case as an alternative method? Or is that what the string compare standard function does.
  5. Detrius macrumors 68000

    Sep 10, 2008
    Asheville, NC
    There's nothing to stop you from doing an address compare, but there's no reason you would want to here. That's what my metaphor about the two balls in your two hands is supposed to convey. You have two different arrays. An address compare will always come up not-equal, no matter what's in the arrays. It will only tell you if you have two pointers that reference the same chunk of memory. That is, it will tell you if you have two hands on the same ball--not whether you are holding two identical balls.

    The string compare function is better than my simple example in that it doesn't have the string-length bug, but this is roughly what it would do--walk through the strings comparing each character one at a time.

    This is where an address compare would work:

    const char string_one[] = "a string";
    const char string_two[] = "a string";
    char * a_pointer = string_one;
    if ( a_pointer == string_two )
        printf("this is not possible");
    if ( a_pointer == string_one )
        printf("we stored the address of the first string.");
    About the only way an address compare is going to be useful is in a highly contrived example. Chances are that it's not what you want. It's possible that it could be useful at some point, but there's probably a clearer way to write your code if you're ever thinking it's a good idea.

    One exception: you should always do an address compare inside operator=() in C++, as there's no reason to bother with the assignment code if you're assigning an object to itself. But really, that's just a safeguard.

Share This Page