1

I'm trying to figure out where/how to define the root node of my trie in dictionary.c. I have declared my struct node as follows:

typedef struct node 
{
    bool is_word;
    struct node* children[27];
}
node;

To my understanding, I need a (pointer to) the root node that can be used by both load and check, so I can't define it in load or check. However, when I try to do

node* root = malloc(sizeof(node));

outsude of either function, the compiler throws me "initializer element is not a compile-time constant".

How do I fix this?

9

It's actually a simple fix. Since it's something that you would either know or not know, here it is. You declare the pointer variable as a global outside of a function, but you malloc space for it inside of main or another function. It's the = malloc(...) part that's killing you here. You can't do that outside of main or another function.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Do you then need to malloc() space for the root node more than once? Can I just malloc() space for it in load and then clear it in unload? Or if I reference it more than once, will I need to malloc() space for it again in other functions which interact with the root node (like check) – Peter Walton Nov 11 '17 at 21:13
  • This solved a hell of headache. Thanks! – pankaj Oct 7 '18 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .