So I'm doing pset5, speller.

I'm storing the words in the dictionary in a trie, whose root is a pointer to a node, called root. root is a global variable first initialized to NULL. root is first used in the function load, which allocates memory to root.

Now, when the next function that uses root is called, the value of root is shown to be 0x0. I can't fathom why this is happening.

Also, I apologize if a similar question has been asked in the past. I have searched for quite a while with no avail, but that might have been due to some ignorance or misunderstanding on my part.

Extra info I'm not sure whether or not you need:

  • All my work is in the file dictionary.c, which defines functions that are called by speller.c. Only the functions in dictionary.c use root.

  • Here is how I define a node:

       typedef struct node
       bool is_word; //set to true if end of valid word
       struct node* children[27];//This is an array, each element of which is pointer to a node.

    root is then initialized as

    node* root = NULL;

    (All code mentioned above is at the top of dictionary.c, right after all the #include stuff.)

  • Here is what load looks like:

    bool load(const char *dictionary)
    FILE* fp = fopen(dictionary, "r");
    node* root = malloc(sizeof(node));
    /**following code is probably irrelevant, seeing it only 
    *mentions root at all when it assigns node* now the value of root.
    *but here it is just in case.
    return false;
    char word[LENGTH+1]={'\0'};
    while(fscanf(fp, "%s", word)!=EOF)//get words until EOF.
    //we want to store a pointer to where we are now.
    node* now = root;
    int x;
    for(int i = 0; word[i]!='\0'; i++)//idk if fscanf actually puts the '\0' in word[]
            x = tolower(word[i]) - 'a';
        else if(word[i]=='\'')
            x = 26;//change this to non magic no. whatever. ugh.
        if (now->children[x]==NULL)
            now->children[x] = malloc(sizeof(node));
                return false;
        now = now->children[x];
    }//close for loop
    now->is_word = true;//to mark valid word.
    s++;//global var, counter for size of dictionary.
    }//close while loop
    return true;
  • To check for any accidental reassignments that might have occurred during load, I had load print me the address root pointed to in every loop inside, and in the last line of load. The value was always the same.

1 Answer 1


It looks like you are declaring what is called a "shadow variable".

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

By doing this inside of load(), the code has declared a new, local variable called root. It will take precedence, hiding or masking the global var root. When load exits, the local shadow var will disappear and the global var root will reappear, still set to NULL.

To fix this, remove the node * from the line above. Doing so will change it from a declaration to an assignment statement.

There may be more issues, but this is the immediate problem. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • (facepalms) There I go making simple mistakes again. Thank you!
    – SMT
    Oct 22, 2017 at 17:01

You must log in to answer this question.

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