0

My implementation of load and check keep giving a seg fault when speller is executed. I've looked through questions with a similar problem, and I've tried everything - I've made sure that head is not shadowed, and that load is not passed an uppercase letter (although this seems counter-intuitive, since all letters in the dictionary are lowercase :/). I still cannot find a solution! I would greatly appreciate some help. Here's my code:

node* head;

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */

bool load(const char *dictionary)
{
    FILE* dic = fopen(dictionary, "r");
    FILE* fp = fopen("testfile.txt", "w");

    if (dic == NULL)
        return false;

    head = NULL;
    head = malloc(sizeof(node));
    if (head == NULL)
        exit(0);
    head -> is_word = false;
    for (int i = 0; i < 27; i++)
        head -> children[i] = NULL;

    node* trav = head;

    for (int b = fgetc(dic); b != EOF; b = fgetc(dic))
    {
        char letter = tolower((char) b);

        if (letter == '\n')
        {
            trav -> is_word = true;
            trav = head;
            num_words++;
        }
        else if (trav -> children[letter - 'a'] == NULL) //SEG FAULT happens here//
        {    
            if (letter == '\'')
            {    
                trav -> children[26] = malloc(sizeof(node));
                trav = trav -> children[26];
            }
            else
            {    
                trav -> children[letter - 'a'] = malloc(sizeof(node));
                trav = trav -> children[letter - 'a'];
            }
        }
        else 
        {   
            if (letter == '\'')
                trav = trav -> children[26];
            else 
                trav = trav -> children[letter - 'a'];
        }
    }
    fprintf(fp, "%u", num_words);
    fclose(fp);
    fclose(dic);
    return true;
}

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    node* trav = head;
    char lcword[strlen(word) + 1];

    for (int i = 0; i < strlen(word) + 1; i++)
        lcword[i] = tolower(word[i]);

    for (int j = 0; j < strlen(lcword); j++)
    {
        if (lcword[j] == '\'')
        {
            if (trav -> children[26] != NULL)
                trav = trav -> children[26];
            else
                return false;
        }
        else
        {
            if (trav -> children[(lcword[j]) - 'a'] != NULL)
                trav = trav -> children[(lcword[j]) - 'a'];
            else
                return false;
        }
        if (j == strlen(lcword) - 1)
        {
            if (trav -> is_word == true)
                return true;
            else
                return false;
        }
    }
    return false;
}
1

What happens here else if (trav -> children[letter - 'a'] == NULL) if letter is apostrophe (ascii 39)?

1
  • Thank you so much! I can't believe I didn't see that. My now my load and check work perfectly! Now on to unload... – Jason_V Jun 25 '17 at 16:24

You must log in to answer this question.

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