在以下工作代码中;而是采用*tofind
,如果我直接使用比较为什么直接比较字符串失败,但成功使用char *
if(*argv[i] == "and")
失败。
为什么会这样?
/**
* Find index of the word "and"
* ./a.out alice and bob
*/
int main(int argc, char **argv) {
int i = 0;
char *tofind = "and";
while (argv[i] != NULL) {
if(*argv[i] == *tofind) {
printf("%d\n", i + 1);
break;
}
++i;
}
return 0;
}
您可以直接比较'char'不串字符串比较使用'strcmp'看更多http://www.cplusplus.com/reference/cstring/strcmp/ –