Hey guys! I have been working on pset5 for a while and I keep on obtaining a seg fault. I have alreay run valgrind but it gives me no error. I cannot really see where is the problem. Any help will be really appreciated! Thanks in advance
typedef struct node
{
bool is_word;
struct node* alpha[27];
}
node;
unsigned int dicSize = 0;
struct node* root = NULL;
bool check(const char* word)
{
node* next = root;
unsigned int k = 0;
int i = 0;
while(k != '\0')
{
k = word[i];
// switch to lowercase
if(k >= 65 && k <= 90 )
{
k = tolower( k);
}
if((k <= 122 && k >= 97 ) || (k == '\''))
{
if(k == '\'')
k = 123;
if(next->alpha[k - 97] == NULL)
{
return false;
}
else
{
next = next->alpha[k - 97];
}
}
i++;
}
return next->is_word;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
FILE* fmine = fopen(dictionary, "r");
if (fmine == NULL)
{
return false;
}
node* root = malloc(sizeof(node));
node* next = root;
unsigned int read= 0;
do
{
read = fgetc(fmine);
if(read == '\'')
read = 123;
if(next->alpha[read - 97] == NULL)
{
next->alpha[read - 97] = malloc(sizeof(node));
}
else (next = next->alpha[read - 97]);
}while (read != EOF && read != '\n');
if(read == '\n')
{
next->is_word = true;
dicSize++;
next = root;
}
fclose(fmine);
return true;
}