如果声明不起作用?

时间:2012-09-14 07:54:03

标签: c

wordCur是大写字母的字符串,而dictionary是一个字符串数组,无论我输入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;
    }
}

3 个答案:

答案 0 :(得分:2)

  

wordCur是一串大写字母

int checkValid (char wordCur,int dictLen, char dictionary[dictLen])

不,wordCur是一个字符。不是字符串。 C中的字符串表示为字符数组,由值为0的字符终止。您需要一个指针参数char *wordCur

答案 1 :(得分:0)

wordCur是一个大写字母字符串,而dictionary是一个字符串数组

试试这个:

int checkValid (const char *wordCur,int dictLen, const char *dictionary[])

顺便说一下,即使在你找到了你想要的东西之后,你仍然继续寻找,而且对于弦乐来说,comaprison是错误的。我建议:

for (i = 0; i < dictLen; i++){  
    if (strcmp(wordCur, dictionary[i]) == 0){  
        valid = 1;  
        break;
    }  
}  

答案 2 :(得分:0)

您的代码应该看起来更像这样:

int checkValid(const char *wordCur,       // word to search for (string)
               int dictLen,               // no of entries in dictionary
               char dictionary[][7])      // dictionary (array of strings)
{
    int valid = 0;
    int i;
    for (i = 0; i < dictLen; i++)
    {
        if (strcmp(wordCur, dictionary[i]) == 0)
        {
            valid = 1;
            break;
        }
    }
    return valid;
}