I've declared a trie in the .h file with
typedef struct node
{
bool is_word;
struct node* children[27];
}node;
and also declared
node* root;
Above the .c file. I've always receive a feedback of the fault. I think the problem lies in the "if (ptr->children[key_i] == NULL)" line but didn't know what exactly is the problem, anyone please help and give advice please.
bool load(const char* dictionary)
{
FILE* dict = fopen(dictionary,"r");
if (dict == NULL)
return false;
while (!feof(dic))
{
char* word = malloc(sizeof(char[LENGTH+1]));
fscanf(dict, "%s", word);
printf("%s\n", word);
node* ptr = root;
for (int i = 0, n = strlen(word); i < n; i++)
{
int key_i = toupper(word[i] - 'A');
if (ptr->children[key_i] == NULL)
{
node* new = malloc(sizeof(node));
ptr->children[key_i] = new;
ptr = ptr->children[key_i];
if (i == (n - 1))
{
ptr->is_word = true;
}
}
else
{
ptr = ptr->children[key_i];
if (i == (n -1))
{
ptr->is_word = true;
}
}
}
}