My implementation of load uses a trie and when I attempt to access the bool is_word
I get a segfault. Can you guys help me out? Any suggestions on how to improve my code would be amazing. Thanks everyone!
bool load(const char* dictionary)
{
node* root = malloc(sizeof(node));
for(int i = 0; i < 27; i++) {
root->children[i] = NULL;
}
node* current = malloc(sizeof(node));
current = root;
for(int i = 0; i < 27; i++) {
current->children[i] = NULL;
}
FILE* dict = fopen(dictionary, "r");
if(dict == NULL) {
return false;
}
char letter = 'a';
do {
letter = fgetc(dict);
if(letter == EOF) {
current->is_word = true;
break;
}
if(letter == '\n') {
current->is_word = true;
current = root;
} else {
int pos = tolower(letter) - 'a';
if(current->children[pos] == NULL) {
current->children[pos] = malloc(sizeof(node));
current->children[pos] = NULL;
}
current = current->children[pos];
}
}while(letter != EOF);
fclose(dict);
return false;
}