【发布时间】:2021-10-12 11:08:06
【问题描述】:
这里我写了一个代码来判断一个词是否可用。
#include <stdio.h>
#include <string.h>
char *names[] = {"Sushant", "Jhon", "Robin", "Mark", NULL};
int search(char *p[], char *names);
int main(void){
if(search(names, "Sushant") != -1){
printf("Sushant is in this list");
} else{
printf("Sushant is not in this list.");
}
return 0;
}
int search(char *p[], char *name){
register int t;
for(t = 0; p[t]; t++){
if(!strcmp(p[t], name)){
return t;
} else{
return -1;
}
}
}
代码运行良好。但问题是这样的。为什么它在工作?我使用strcmp 来比较数组和给定名称,但我也在strcmp 的开头使用! 来反转语句。问题是应该只有strcmp 而不是!strcmp 你能告诉我为什么在这里使用!strcmp 吗?
【问题讨论】:
-
strcmp在传递的字符串匹配时返回零。 -
您是否阅读过您不确定它们如何工作的函数的文档?
标签: c for-loop c-strings strcmp function-definition