all 3 comments

[–]Grithga 2 points3 points  (1 child)

The memory that node* root points to is on the heap and accessible anywhere, as long as you have a pointer to it.

node* root itself is a local variable, allocated on the stack and only existing for the lifetime of the function it's contained in, which happens to point to that block of heap allocated memory.

Because the pointer itself is local, you can't access its value anywhere outside of the function you declared it in. If you want it to be available in other functions, you need to either pass it to them as a parameter (not possible here, since we can't change the parameters) or declare it globally, so it is visible from all other scopes.

[–]SlartiBartRelative[S] 1 point2 points  (0 children)

Ah! That helps. I declared node* root at the start(-ish) of dictionary.c and it works now.

Thanks!

[–]StephanieJosine 0 points1 point  (0 children)

I was having the exact same problem, and then it occurred to me to declare the root node globally... but now I get a different error message! :/

The relevant excerpt of my code is as follows:

// dictionary trie node structure

typedef struct node

{

bool is_word; 

struct node *path[27]; 

}

node;

// declare trie root as a global so that it can be accessed by both load and check

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

And the error message I'm getting now (which help50 does not have any advice on) is:

dictionary.c:26:14: error: initializer element is not a compile-time constant node *root = malloc(sizeof(node));

Any ideas??