0

My code has a segmentation fault and always prints this out:


MISSPELLED WORDS

dictionary.c:111:19: runtime error: member access within null pointer of type 'node' (aka 'struct node')
Segmentation fault

The error is here:

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *cword = hashtable[hash(word)];
    int length = strlen(word);
    char lword[length];
    strcpy(lword, word);
    for (int i = 0; i < length; i++)
    {
        lword[i] = tolower(word[i]);
    }
    while (cword->/*ERROR HERE*/next != NULL)
    {
        if (cword->word == lword)
        {
            return true;
        }
        cword = cword->next;
    }
    return false;
}

I've found out that check doesn't recognize hashtable, and so cword is NULL.

0

You should loop while the current pointer is not NULL instead of checking current node's next member.

Also, strings cannot be compared using ==, check out strcmp. Don't forget the null terminator '\0', and have space for that one in your array.

edit: Missed another thing: should be char lword[length + 1];, with room for a null terminator (lword[length] = '\0';). The strcpy would be redundant then.

2
  • Now it says free(): invalid pointer Aborted Jun 5 '19 at 4:19
  • Not enough space for the null terminator, missed that one. But if that does not fix it, I don't see a free in that code, so unless you messed up something by fixing check, the problem would be in load or unload. Maybe a different question then?
    – Blauelf
    Jun 5 '19 at 7:48

You must log in to answer this question.

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