我试图通过 fooArray 循环搜索 foo 的每个条目并在适当的条目中增加 fooCount 。正如它所代表的字符串: HAPY \0 已成功输入到 foo 和 fooArray[2] 但我无法让 fooArray[2] == foo 评估为真。
我应该为此使用 string.h 和 strcmp 还是 strstr ?
为什么 fooArray[2] == foo 不评估为真?
#include <stdio.h>
#include <stdlib.h>
int main(void){
char foo[5] = "HAPY";
static char fooArray[4][5] = {"abc", "123", "HAPY", "some"};
int fooCount[4] = {0};
int n;
printf("size of fooArray: %d\n", ( sizeof(fooArray) / sizeof(fooArray[0])));
for (n = 0; n < sizeof(fooArray) / sizeof(fooArray[0]); n++) {
if (fooArray[n] == foo) {
fooCount[n]++;
break;
}
}
for (n = 0; n < sizeof(fooArray) / sizeof(fooArray[0]); n++)
printf(" fooArray[%d] = %s \n", n, fooArray[n]);
for (n = 0; n < sizeof(fooCount) / sizeof(fooCount[0]); n++)
printf(" fooCount[%d] = %d \n", n, fooCount[n]);
system("pause");
return 0;
}