我試圖通過fooArray搜索foo的每個條目並在相應條目中增加fooCount。由於它代表字符串:H A P Y \ 0已成功輸入到foo和fooArray [2]中,但我無法將fooArray [2] == foo評估爲true。C - 嘗試在charArray [n] []中查找char []並記錄n的值
我應該爲此使用string.h和strcmp或strstr?
爲什麼不將fooArray [2] == foo評估爲true?
#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;
}
一個非常完整在C的字符串概述,請參閱linux終端上的'man string'或這個:http://linux.die.net/man/3/string(以減輕你不得不穀歌的人字符串:) –