wordCur是大寫字母的字符串,字典是一個字符串,數組不管我投入wordCur,我總是返回0。如果聲明不起作用?
編輯:我更新的代碼一點點,並增加了刪節某些情況下程序其餘部分的版本。正如本文所示,它只是崩潰當它到達checkValid
int main() {
FILE *ifp;
ifp = fopen("dictionary.txt", "r");
int* lDist[26];
int* lUsed[26];
int dictLen;
int i;
fscanf(ifp, "%d", &dictLen);
char dictionary[dictLen][7];
char* letters[7];
int scoreCur = 0;
int scoreHi = 0;
char wordCur[7];
char wordHi[7];
int isWord = 0;
//reads the dictionary into the array
for (i = 0; i < dictLen; i++) {
fscanf(ifp, "%s", &dictionary[i]);
}
scanf("%s", wordCur);
isWord = checkValid(wordCur, dictLen, dictionary);
if (isWord == 1) {
scoreCur = calcScore(wordCur);
}
//fclose(ifp); not sure why, but this causes a crash
return 0;
}
int checkValid (char *wordCur,int dictLen, char dictionary[dictLen]) {
int valid = 0;
int i;
for (i = 0; i < dictLen; i++){
int helper = strcmp(wordCur, dictionary[i]);
if (helper = 0){
valid = 1;
}
}
這不是有效的C:'dictionary'既不是數組,也不是一個指針。 – cnicutar
我想你錯過了一個'*'之前的單詞**字典** – hmmftg
我懷疑你缺少你的代碼中的一些*字符...並且你可能想要使用strncmp而不是比較指針地址來檢查字符串是否相等 –