I don't understand why strcmp says these strings are different. In gdb they look identical. I can't figure it out and I have searched the forum and didn't have any luck.

Please let me know if I can provide any other info. Thanks in advance

char method[3];
strncpy(method, line, 3); 

char methodcmp[] = "GET";

if  (strcmp(methodcmp, method) != 0)
    return 1;

1 Answer 1


method is 3 chars long and you need at least 4 characters to store the string "GET" {'G', 'E', 'T', '\0'}. so technically your variable method is not actually a string (it doesn't terminate with a '\0').

even if the first 3 characters of method and methodcmp are identical, there's no guarantee that method[3] (which is out of method's bounds) will be storing '\0' to match methodcmp[3] which is in methodcmp's range at the moment strcmp is called and you should never rely on that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .