I am getting segmentation faults in the PSET 6, the load function I have already searched google but I can't see where I've been wrong, I've been struggling with this function all day with no luck

I am posting the whole code because even though load() is the issue, I have defined some constants and structs at the very top

also, the hash function isn't very good but I'll improve it when the code is actually working, and I'll pick a better number than 100 buckets

I think the problem may have something to do with syntax, I've had lots today regarding arrays of nodes :/ my head says if I declare node* name[how_many_nodes] means every index name[index] has both a char[] and a next* pointer, am I wrong? because when I see other's code, they don't type name[index] -> next == NULL but name[index] == NULL (or = if assigning)

this is my code https://gist.github.com/anonymous/ca9c95f07e436289dd3a

thanks beforehand!

1 Answer 1


The problem is that you're trying to assign content to vars that don't exist. Look at the following:

    if (hashtable[index] == NULL)
        strcpy(hashtable[index] -> word , buffer);
        hashtable[index] -> next = NULL;

hashtable[] is an array of pointers to nodes, not nodes. At this point (the very first word), the pointer is null. That means that there is no struct at hashtable[index] and both word and next do not exist. They haven't been allocated. Before you can do the strcpy, you need to malloc a node to hashtable[index] before doing the string copy and the null assignment to next.

You actually do the malloc in the following else clause. Wouldn't it be more efficient to first create a new_node, strcpy the word in, and set the next pointer to null, and then figure out where to put it in the hashtable[] linked lists?

No guarantees that there aren't other issues, but that wasn't the question. ;-) This should get you going.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • thank you so much!
    – Rafael
    Mar 13, 2016 at 2:53
  • Can I still used the hashtable[index] as the head?
    – Rafael
    Mar 13, 2016 at 3:01
  • Of course. You just need to point the hashtable[index] at the new node instead of null as you go.
    – Cliff B
    Mar 13, 2016 at 3:02
  • thank you again! I will rewrite my code tomorrow from the beggining because I feel like I've already screwed too hard. Let me see if I understand right: the array[] has for example 100 indexes, each index is only a pointer, and it will point to the first node which will point to the next node and so on...? therefore in the array[index] I can only fit an address
    – Rafael
    Mar 13, 2016 at 3:14
  • correct. each pointer points to a node (think new_node). From there, *->next points to the next node.
    – Cliff B
    Mar 13, 2016 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .