0

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, ",");
    }

2 Answers 2

9

Well, if it's

char pch[64];

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 calling strcmp() 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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tom "map[i].name has a known size of 64" so he's point still holds.
@Tom You just can't put 64-char-length string in a 64-length char array.
5

If one or both your arrays have an exact size of 64, you are missing the final '\0' ending the string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.