C++ - strcmp() does not work correctly?


There's something really weird going on: strcmp() returns -1 though both strings are exactly the same. Here is a snippet from the output of the debugger (gdb):

    (gdb) print s[i][0] == grammar->symbols_from_int[107][0]
    $36 = true
    (gdb) print s[i][1] == grammar->symbols_from_int[107][1]
    $37 = true
    (gdb) print s[i][2] == grammar->symbols_from_int[107][2]
    $38 = true
    (gdb) print s[i][3] == grammar->symbols_from_int[107][3]
    $39 = true
    (gdb) print s[i][4] == grammar->symbols_from_int[107][4]
    $40 = true
    (gdb) print s[i][5] == grammar->symbols_from_int[107][5]
    $41 = false
    (gdb) print grammar->symbols_from_int[107][4]
    $42 = 0 '\0'
    (gdb) print s[i]
    $43 = (char * const&) @0x202dc50: 0x202d730 "Does"
    (gdb) print grammar->symbols_from_int[107]
    $44 = (char * const&) @0x1c9fb08: 0x1c9a062 "Does"
    (gdb) print strcmp(s[i],grammar->symbols_from_int[107])
    $45 = -1


Any idea what's going on?

Thanks in advance,
Onur
Huh? whats a debugger? give me some code ... im the debugger lol
whats a debugger?
Quiet, you.

Hmm... This is indeed strange. What if you use your own strcmp()? Does it produce the same behavior?
Hmm... This is indeed strange. What if you use your own strcmp()? Does it produce the same behavior?


No, it does not. I just tried it and the string comparison function I wrote worked correctly at the same point. So obviously it's a problem with strcmp(). Thanks for your interest.
Weird. It seems really unlikely, nay, impossible, for strcmp() to be bugged. What about using it in a different program?
Make sure there isn't a stray '\n' or something (it might not show on print, I don't remember).
Make sure there isn't a stray '\n' or something (it might not show on print, I don't remember).
Yes, but it would show up when comparing each of the elements. The fifth elements of both arrays are equal, and s[i][4] is zero, so there are no hidden characters.
Post your strcmp code and your code using it and the libC strcmp. This is weird.
Well, the strcmp() I first used was the standard one from string.h . The string comparison function I wrote is as follows:

1
2
3
4
5
6
7
8
bool mystrcmp(const char *s1, const char *s2) {
        int i = 0;
        do {
                if(s1[i] != s2[i])
                        return false;
        } while(s1[i++] != '\0');
        return true;
}


As I told, the one I wrote works correctly at the point strcmp() fails. Actually I suspect something is wrong with g++, since it's giving nonsense errors to me now. I will reboot my computer now and try to upgrade g++, then I'll report back if the problem goes on. Thanks for your interest.
IIRC strcmp is more like this:

1
2
3
4
5
6
7
8
9
10
/* strcmp: compare the C-strings s1 and s2
 * Arguments:    const char* s1: a string
 *               const char* s2: a second string, to be compared with s1
 * Return value: 0 if the strings are exactly the same, non-zero otherwise.
 *               If the return value is non-zero; it is the value of the first character in s1 - the first character in s2 which do not match.
 */
int strcmp(const char* s1, const char* s2) {
    for (; (*s1 == *s2) && (*s1 != '\0'); s1++, s2++);
    return (*s1 == *s2) ? 0 : *s1 - *s2;
}


If you called it with this:
strcmp("string1", "string2");
then it would return -1 as '1' - '2' (the ASCII characters and the numbers) = -1.
You would get the same result from ('1' - '0') - ('2' - '0') except in numeric form.
Last edited on
Topic archived. No new replies allowed.