char* mystr = calloc(25, sizeof(char));
fgets(mystr, 25, stdin); // I enter "6 7 *" in here, without the quotes
char* tok;
tok = strtok(mystr, " ");
while (tok != NULL) {
if(strcmp(tok, "*") == 0)
//It never meets this condition, but I don't understand why
else
//do something else here
tok = strtok(NULL, " ");
}
的问题是,strcmp(tok, "*")
不会返回作为平等的,即使tok
从原始字符串的星号读取。我不明白为什么它永远不符合这个条件。麻烦的strcmp(c)中
'sizeof(char)'是'1'。 –
了解如何使用调试器(如'gdb')。也许试着'strncmp(tok,“*”,1)' –
逐行扫描调试器中的代码,并逐行查看'strtok'返回的内容。 –