This is the code for my load function
bool load(const char *dictionary)
{
//open dictionary
FILE *inptr = fopen(dictionary, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", dictionary);
return false;
}
//get the word
char word[LENGTH +1];
while (fscanf(inptr, "%s", word) != EOF)
{
//load up node
node *new_entry = malloc(sizeof(node));
//if we run out of memory
if (new_entry == NULL)
{
free(new_entry);
return false;
}
//else, successful
strcpy(new_entry -> word, word);
//hashfunction; a is hastable[0], b is hastable[1]
int hashtable_index = word[0]-97;
//link nodes in linked list
if (hashtable[hashtable_index] -> next == NULL)
{
hashtable[hashtable_index] -> next = new_entry;
}
else
{
new_entry -> next = hashtable[hashtable_index];
hashtable[hashtable_index] = new_entry;
}
free(new_entry);
}
return true;
}
When we load in our first word into our dictionary, my "head" (aka hashtable[hashtable_index]) points to nothing yet. However, I know later on throughout the process, the "head" WILL be pointing to my linked list of nodes. In order to address this, I put an IF statement saying that if hashtable[hashtable_index] -> next is NULL, then the "head" should point to the new entry. If it is not NULL, then I have code that will (hopefully) stitch the new_entry at the front of the linked list, while not losing the "head" pointer to the linked list.
I get segmentation fault for accessing hashtable[hashtable_index] -> next, which is what I expect when no nodes are loaded in yet for "head" to point to. How do go about expressing this IF statement differently?