0

Could someone please review this code as I am not sure why am I getting segmentation fault while trying to use ' in words in a dictionary. I am attaching the code of my load function.

/////////////////////////////////////////////////// usefeull.h :

#include <stdbool.h>

typedef struct tries 
{
    bool end_Word;
    struct tries* children[27];
}
tries;
//////////////////////////////////////////////////
bool load(const char* dictionary)
{

 tries* root;

    //open file for reading
    FILE *dict = fopen(dictionary,"r");
    if(dict == NULL)
    {
        printf("Could not open file!!");
        unload();
        return false;
    }
   int letterIndex = 0;

    root = malloc(sizeof(root));
    tries* newL = root;

    int word = 0;
    //go through every single character of every single word in the dictionary

for(int c = fgetc(dict); c != EOF; c = fgetc(dict))
{

     //setting Index
     letterIndex = 55; 

        //check if the character is valid

            //set the letter index
     //setting index for ' char
     if (c == 39)
     {
        letterIndex = 26;
     }
    //getting index of alphanumeric char
    if (isalpha(c))
     {
        letterIndex = (c - 97) & 25;

     }

        //allocate memory for every node

        //if index not changed , new word and reset to the root
    if(letterIndex == 55)
    {

        word++;
        printf("%d\n" , word);
        newL -> end_Word = true; 
        newL = root;

    }
    //if letter found
    //if pointing to NULL
    else if(newL->children[letterIndex] == NULL)
    {

        newL-> children[letterIndex] = malloc(sizeof(newL));
        newL = newL->children[letterIndex]; 
        printf("being created\n");
    }
    //if already pointing 
    else 
    {
        newL = newL -> children[letterIndex];
        printf("position already exist\n");
    }
}

   return true;
}
1
  • sizeof(root) does not look right - should be sizeof(tries)
    – jacmoe
    Commented Jan 19, 2017 at 21:27

1 Answer 1

0

I have figured it out myself. Thanks a lot for you interest :D

1
  • Can you please edit your answer and explain the problem and solution so others can benefit? Remember to accept your answer (when the system allows) to remove from unanswered question pool. thanks. ;-)
    – Cliff B
    Commented Sep 2, 2016 at 21:15

You must log in to answer this question.

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