这是一个电子词典程序。但是在这个程序中,dict[i][0]如何与空格进行比较。而且它会是真的。以及最后一个if部分如何与dict[i][0]进行比较到太空。请任何人解释一下。
#include<stdio.h>
#include<string.h>
int main(void)
{
char dict[][2][40] = {
"house","a place of dwelling",
"car","a vehicle",
"computer","a thinking machine",
"program","a sequence of instruction",
"",""
};
char word[80];
int i;
printf("Enter word: ");
gets(word);
i = 0;
while(strcmp(dict[i][0], "")){
if(!strcmp(word, dict[i][0])){
printf("Meaning: %s", dict[i][1]);
break;
}
i++;
}
if(!strcmp(dict[i][0], ""))
printf("Not in dictionary\n");
return 0;
}
最佳答案
在您的代码中,
strcmp(dict[i][0], "")
不会将dict[i][0]
与)空格进行比较,而是对空字符串进行检查,该字符串是哨兵定义的数组的值。
另请注意,dict[i][0]
的类型是 char[40]
,它会衰减为 char *
,所以无论如何,这都是 strcmp()
的有效参数。
关于c - 这个字符串如何运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41032898/