I posted this to the CS50 Subreddit with no help, so i thought i'd post here and try to get some help. My load function is not working, and keeps producing a segmentation error at (hashtable[index]->next = new_node;) and i've tried everything to figure out what is wrong with it to no avail. Any help would be very much appreciated!
(I'm also struggling to figure out how / when to return true, so any tips regarding that would also be great)
bool load(const char *dictionary)
{
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
node *hashtable[26];
node *last;
char word[LENGTH + 1];
bool letterused = false;
int charusednow = 0;
int charusedbefore = 0;
// node *head = NULL;
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
fprintf(stderr, "The Dictionary does not Exist");
return false;
}
while (fscanf(dict, "%s", word) != EOF)
{
charusednow = word[0];
if (charusednow != charusedbefore)
{
letterused = false;
}
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
unload();
return false;
}
strcpy(new_node->word, word);
int index = word[0] % 26;
if (letterused == false)
{
hashtable[index]->next = new_node;
letterused = true;
}
else
{
last->next = hashtable[index]->next;
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
charusedbefore = word[0];
}
return true;
}