When my code runs I get a seg fault. I ran GDB and it crashes where you can see the ***. The strange thing is that in GDB I do not see any value in buffer before the crash, whereas it should have read a word from the dictionary. Really don't know what's up. Tips much appreciated, CB
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// open dictionary
FILE* inptr = fopen(dictionary, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", dictionary);
return false;
}
// buffer
char buffer[LENGHT + 1];
// store each word in table
while(fscanf(inptr, "%s", buffer) == 1)
{
// make new node & insert read word
node* new_node = malloc(sizeof(node));
new_node->next = NULL;
strcpy(new_node->word, buffer);
// hash the word
int key = hash_it(buffer);
// if indexed location is empty then insert node there
*** if(hashtable[key]->next == NULL)
{
hashtable[key] = new_node;
}
// else insert node afer indexed location
else
// set pointer of new node to the next node
new_node->next = hashtable[key]->next;
// set pointer of indexed location to new node
hashtable[key]->next = new_node;
// add 1 to dictionary counter
count_words++;
}
return true;
}