輸出說,當文件fileIn的文字按順序正確打印時,所有數組值都是「焦慮」。我的問題可能在realloc()中?fscanf變量設置不正確。可能realloc()
ReadIn = malloc(sizeof(char) * 50);
while(fscanf(fileIn, "%s", ReadIn) != EOF){
wordlist = (char **)realloc(wordlist, (numwords + 1) * sizeof(char*));
printf("%s\n",ReadIn);
wordlist[numwords] = ReadIn;
numwords++;
}
for(i = 0; i < numwords; i++){
printf("%d %s\n", i , wordlist[i]);
}
輸出:
This
is
a
journey
into
fear
and
anxiety.
0 anxiety.
1 anxiety.
2 anxiety.
3 anxiety.
4 anxiety.
5 anxiety.
6 anxiety.
7 anxiety.
Program4.c:在函數'main'中: Program4.c:49:warning:隱式聲明函數'strdup' Program4.c:49:warning:賦值使得整型指針沒有轉換 – Jared
它可以工作!謝謝。我只是不喜歡這些錯誤。使用迂迴ansi和Wall – Jared
@Jared:'#include'應該得到'strdup()'的正確聲明。 –