1
我試圖讓我自己實現一個特里樹來提交C中的單詞列表,通過在chars數組中存儲chars,然後訪問下一個節點來存儲下一個數組,每個節點包含在但是當我調試它時,似乎與下一個節點數組的連接丟失,因爲它表示它爲空。爲什麼我的指針數組在C中有一個分段函數?
這是結構:
typedef struct node {
char c[ALLCHAR];
struct node *next[ALLCHAR];
} listword;
listword *head;
listword *current;
而這正是實現:
head = malloc(sizeof(listword)); //Initialize the head
current = head; //Pass the pointer to current
dict = fopen(dictionary, "r"); //This is the source of the words
if(dict==NULL) {
fclose(dict);
return 1;
}
//Here I iterate char by char in the dict
for(int c=fgetc(dict);c!=EOF;c=fgetc(dict))
{
int myc=tolower(c)-97; //The index equivalent to the char
if(c=='\n') {
current->c[27]='\0'; //The end of the word
current=head; //Pass the start pointer to the current
} else {
current->c[myc]=c; //Here it puts the char in its position
current=current->next[myc]; //Here I identify my problem, even
//when I already initialized next it
//appears as no value
if(!current) {
current=malloc(sizeof(listword)); //Here I initiliaze next
//if haven't initiliazed yet
}
}
}
請幫我在這個問題上,在此先感謝。
現在是學習如何使用調試器來調試你的代碼的好時機。一旦你這樣做了,並收集了一些相關的細節,請編輯你的問題,併發布你發現的 – OldProgrammer
你確定'0 <= myc
yano
該文件是否包含換行符?如果是這樣,您將使用''\ n' - 91'作爲數組索引 - kaboom!至少在它工作之前,我建議檢查數組索引。 –