I am trying to create a function that will malloc
my node
pointers but for whatever reason the passed node
when accessed after my function throws an error as if the node
was not given memory at all. Here is my code:
typedef struct node
{
bool is_word;
char chars[N];
struct node *children[N];
}
node;
node *root;
bool alloc_memory(node *n)
{
n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
for (int i = 0; i < N; i++)
{
n->children[i] = NULL;
}
return true;
}
bool load(const char *dictionary)
{
// Initialize trie
bool t = alloc_memory(root);
if (!t)
{
return false;
}
root->is_word = false;
...
}
At root->is_word = false;
I get the following two errors:
runtime error: member access within null pointer of type 'node' (aka 'struct node')
runtime error: store to null pointer of type 'bool'
What am I missing to correctly initialize my node
with malloc
?