1

The only code I have added to the top of my dictionary.c is this

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "dictionary.h"

/**
 * Returns true if word is in dictionary else false.
 */

 // Structure of trie
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

// Vaiables used for the trie
node* root = malloc(sizeof(node));
node* triepointer = root;

However, the compiler tells me that malloc doesn't work as initializer element is not a compile-time constant.

What does the error mean? (googled and it seems that I need to use a constant here, but which part of this line is missing a constant and why?) Also, I see that Zamyla's guide doesn't use malloc when creating the root node. How can root be used if memory isn't allocated for it?

2

You declare node* root; in dictionary.h, but you can't malloc memory for it until you're in dictionary.c. However, by declaring it in your header file, you can use it across all your relevant functions, which is necessary for proper trie implementation.

3
  • Sorry, I meant dictionary.c in my first sentence.
    – Wet Feet
    Feb 22 '16 at 19:12
  • Got it. You have to allocate memory for root within your load function itself to begin creating your trie. You want to make your function declarations in dictionary.h, so declaring the struct node and other global variables takes place there. Does that make sense? Declare in header file; use within functions in dictionary.c. The speller.c file calls on the header file, so if you want to use variables across functions, you have to do it there since you are not to modify speller.c.
    – Peter
    Feb 22 '16 at 19:22
  • Yup that makes sense, thank you!
    – Wet Feet
    Feb 23 '16 at 9:34

You must log in to answer this question.

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