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;
}
sizeof(root)
does not look right - should besizeof(tries)