This is my code:
if(strcmp(pch,map[i].name)==0){
printf("Equal\n");
return 0;
}
pch is read from a file, map[i].name has a known size of 64.
This works great for strings smaller than 64. when comparing these two strings below of size 63:
file11111111111111111111111111111111111111111111111111111111111 and
file11111111111111111111111111111111111111111111111111111111111
everything is peachy and the result as expected is equal, but when these two (of size 64) are compared:
file111111111111111111111111111111111111111111111111111111111111 and
file111111111111111111111111111111111111111111111111111111111111
the return is false.
I thought of doing:
if(strncmp(pch,map[i].name,64)==0){
printf("Equal\n");
return 0;
}
And it does work for strings of exact size of 64, but for strings that
are smaller the result is random.
What kind of quirkiness am i dealing with here?
EDIT: this is the full code:
char * pch;
char tempFilesNeeded[100*64+100];
strcpy(tempFilesNeeded,map[i].filesNeeded);
pch = strtok(tempFilesNeeded,",");
while (pch != NULL)
{
if(strcmp(pch,map[i].name)==0){
printf("Equal\n");
return 0;
}
pch = strtok (NULL, ",");
}
Well, if it’s
then you can’t have 64 visible characters in there, since the last entry is needed for the termination. If you do have
"file111111111111111111111111111111111111111111111111111111111111"in that array, it’s not terminated and callingstrcmp()on it invokes undefined behavior.Also, as a minor point, saying that
strcmp()returns “false” is wrong, since its return is not boolean. It returns the relation between the two first differing characters; if no characters differ the strings are equal, then it returns zero.