ok so i define my structure like this.
struct trie {
struct trie *child[26];
int count;
char letter;
};
the problem is when i try to fill my trie with words i get a segmentation fault. ive been told that the problem is that the child variable isn't pointing to anything and setting them to NULL would fix this. Also creating a second structure would be a good way to achieve this. i am new to C programming and am confused on how to create a second structure to achieve this. any help would be much appreciated.
int addWordOccurrence(const char* word)
{
struct trie *root;
root = (struct trie *)malloc(sizeof(struct trie*));
struct trie *initRoot=root;
int count;
int x=strlen(word);
printf("%d",x);
int i;
for(i=0; i<x; i++)
{
int z=word[i]-97;
if(word[i]=='\n')
{
z=word[i-1]-97;
root->child[z]->count++;
root=initRoot;
}
root->child[z] = (struct trie *)malloc(sizeof(struct trie));
root->child[z]->letter=word[i];
root->child[z]=root;
}
return 0;
}
child. Do you knowmalloc/calloc? Or you create othertries and put them in. Can you show us your code?countis for), but you haven't posted the code for inserting, and that's where the problem is. Please post that code.