1

I wanted to test speller to make sure the dictionary loaded into my trie correctly. However, I received dictionary.c:65:27: runtime error: member access within null pointer of type node' (aka 'struct node')" and I'm not quite sure what this means. Here is my code for the load section:

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
// Define a trie
{
typedef struct node{
    bool is_word;
    struct node *children[27];
} node;

//create a root
node *root;

//allocate memory for root
root = calloc(1,sizeof(node));

//root points to base children nodes
for (int i = 0; i < 27; i ++){
    root->children[i] = NULL;
}

//point temporary curser at root
node *trav = root;

//open dictionary
FILE *dict = fopen(dictionary, "r");

//make sure dictionary opens
if (dict== NULL)
{
    fprintf(stderr, "Could not open %s.\n", dictionary);
    return false;
}

//array to hold dictionary word
char word[LENGTH + 1];

  //read file until end
while (!feof(dict)){
    fscanf(dict, "%s", word);
    //scanning each letter in word
    for (int i = 0; i < strlen(word); i++){
        char letter = word[i];
        char lowletter = tolower(letter);
        int strnum = strcmp(&letter, "\n");
        //if letter is in alphabet
        if (isalpha(letter)){
        //checking to see if node exists
            if (trav->children[lowletter - 'a'] == NULL){
                //if doesn't exist
                //allocating memory for node
                trav->children[lowletter - 'a'] = (node*) calloc(1,sizeof(node));
            }
        //points curser to node
        trav = trav->children[lowletter - 'a'];
        }
        //else if apostrophy
        //checking to see if node exists
        else if (trav->children[26] == NULL){
            //if doesn't exist
            //allocating memory for node
            trav->children[26] = (node*) calloc(1,sizeof(node));
        }
        //points curser to node
        trav = trav->children[26];

        //checking if end of word
        if (strnum == 0){
            //setting bool to true to signify end of word
            trav->is_word = true;
            //moving trav back to root
            trav = root;
        }

    }
}
return true;
}

I haven't changed any of the other files and load is the only section where I have written code. Any help is appreciated!

EDIT I fixed my typo in my code

2

OK, the problem lies in a small error in the code. Let's follow the logic of the program.

First, let's assume that line 38 is while (!feof(dict)){

The code will find a letter's node (or add one) in root. Then it will reposition the trav pointer to that node. OK so far.

Then, a few lines later, it hits this code:

    //points curser to node
    trav = trav->children[26];

Simply put, the code adds one node and advances two nodes into the trie. (I'll let you figure out how it gets so far into the trie.) So, when the third letter is processed, it hits this line:

    //checking to see if node exists
        if (trav->children[lowletter - 'a'] == NULL){

Since trav is NULL, the children don't exist and a seg fault is generated.

It's a simple fix.

There may or may not be other issues, but those will be for later questions. Happy coding!

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

You must log in to answer this question.

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