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):
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.
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.
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(constchar *s1, constchar *s2) {
int i = 0;
do {
if(s1[i] != s2[i])
returnfalse;
} while(s1[i++] != '\0');
returntrue;
}
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.
/* 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(constchar* s1, constchar* 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.