I keep getting a segmentation fault on my load function when using the debugger. Specifically, the line with "if(table[num]->next != NULL)" is what is causing it. I need some help, please.
bool load(const char *dictionary)
{
unsigned int num = 0;
char buffer[LENGTH + 1];
//Open dictionary file
FILE* ptr = fopen(dictionary, "r");
if(ptr == NULL)
{
printf("Can't open dictionary");
return false;
}
//Read string from file one at a time
while(fscanf(ptr,"%s",buffer) != EOF)
{
//Create a new node for each word
node* n =malloc(sizeof(node));
if(n != NULL)
{
strcpy(n->word,buffer);
n->next = NULL;
}
//Hash word to obtain a hash value for each word
num = hash(buffer);
//Check to see if something already at location
if(table[num]->next != NULL)
{
n->next = table[num];
}
//Insert node into hash table at that location
table[num] = n;
//keeps track of how many words in dictionary
count++;
free(n);
}
return false;
}