1
私は文字列をcharの配列に格納し、次の配列を格納するために次のノードにアクセスすることで、Cの単語のリストを提出するために独自のトリーツリーを作成しようとしています。私はそれをデバッグするとき、ノードの次の配列との接続は、それがnullだと言われているので、失われているようです。なぜ私の配列のポインタにCでセグメンテーションfaulがあるのですか?
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!少なくともそれが動作するまでは、私は配列のインデックスをチェックすることをお勧めします。 –