When I try to run my program, a segmentation fault results. I don't think I am accessing memory that I am not supposed to. Is my code fundamentally wrong?
bool load(const char* dictionary)
{
FILE* fp = fopen(dictionary, "r");
if(fp == NULL)
{
printf("Could not open the dictionary\n");
return false;
}
for(int i = 0; i < 26; i++)
{
hashtable[i] = NULL;
}
int hashvalue;
while(!feof(fp))
{
node* new_node = malloc(sizeof(node));
fscanf(fp, "%s", new_node->word);
hashvalue = hash(new_node->word);
//if the list is empty
if(hashtable[hashvalue] == NULL)
{
new_node->next = NULL;
hashtable[hashvalue] = new_node;
wordcount++;
}
//in between
else
{
node* cursor = hashtable[hashvalue];
while(cursor != NULL)
{
if((strcmp((new_node->word), (cursor->word)) > 0 && strcmp((new_node->word), ((cursor->next)->word)) < 0) || (strcmp((new_node->word), (cursor->word)) == 0))
{
new_node = cursor->next;
cursor->next = new_node;
break;
}
cursor = cursor->next;
wordcount++;
}
//reach the end
if(cursor == NULL)
{
new_node->next = NULL;
cursor->next = new_node;
wordcount++;
}
}
}
return true;
}