-3
ここに私が使っている3つの構造があります。たとえば、私のプログラム が最初の単語として 'the'を取得すると、* rt-> str = theとなります。 しかし、次の単語が読み込まれるとき、そのキーは* rt-> strと等しくなり、私はその理由を理解しません。私はプログラマーの初心者で、これは本当に私のトラックに止めてしまった。ルートがデータを変更するのはなぜですか?
struct node {
char *str;
int occ;
struct node *sibling;
struct node *child;
};
struct node* root;
struct node* getNew(char word[100]) {
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->str = word;
newNode->sibling = NULL;
newNode->child = NULL;
newNode->occ = 0;
return newNode;
}
struct node* insert(char key[100], struct node **rt){
if(*rt == NULL) {
*rt = getNew(key);
printf("This is the key in the root: %s\n", (*rt)->str);
return *rt;
}else{
printf("root word: %s\n", (*rt)->str);
exit(0);
}
struct node *leaf = *rt;
int n = 0;
int i;
char w2[100];
strcpy(w2, key);
printf("root word: %s\n", (*rt)->str);
for(i = 0; i < strlen((leaf)->str); i++) {
printf("%c %c \n", (leaf)->str[i], key[i]);
if((key[0] == (leaf)->str[i])) {
n++;
key = key + 1;
printf("key is: %s \n", key);
}
}
if(key[0] == 0) {
printf("key is empty \n");
}
printf("This is the word after for loop: %s \n", key);
exit(0);
}
'if(...)return ..; else exit(); 'は関数の残りの部分を到達不能にします。 – mch
デバッガの使用方法を学びます。あなたのコードは、あなたが望むことをしないたびに毎回尋ねることによって、遠くには行きません。 – rustyx
また、 'getNew'では、' newNode-> str = word; 'おそらくあなたは[' strdup'](http://en.cppreference.com/w/c/experimental/dynamic/strdup)を使いたいでしょう。 –