while (fscanf(file, "%s", word) != EOF)
{
// TODO
node *cursor = malloc(sizeof(node));
if(cursor == NULL)
{
unload();
return false;
}
strcpy("cursor->word",word);
cursor->next = NULL;
int i = hash(word);
if(hashtable[i] == NULL)
{
hashtable[i] = cursor;
sizex++;
}
else
{
node *temp = hashtable[i];
while(temp->next)
{
temp = temp->next;
}
temp->next = cursor;
sizex++;
}
}
1 Answer
Remove the quotation marks around cursor->word
. "cursor->word"
is a pointer to some read-only location where exactly those characters ({'c', 'u', 'r', ...}
), together with a null terminator, are stored. When strcpy
tries to copy to that location, you get this nice segfault.
cursor->word
instead is the word
member of the struct cursor
points to.
BTW, better prepend words to the front of the linked list. Order does not matter, and inserting at the front is a constant-time operation, while appending at the end requires you to walk the whole list so far.