I've been working very long on this and I can't find a way out. I've created a node struct and a hash struct. Like this:
typedef struct node {
char value[45];
struct node* next;
}node;
typedef struct hash {
int count;
struct node* head;
}hash;
My dictionary consists of an array of 26 hash structs. The array is called alfabet and I've created a global pointer to alfabet (hash *alfabet). Then I mallocced 26 structs for it inside my load function. I have a separate function that returns a node. This node is equal to temp. When I insert nodes in a hash struct I do the following:
if (!alfabet[letter].head) {
alfabet[letter].head = temp;
alfabet[letter].count = 1;
}else {
temp -> next = alfabet[letter].head;
alfabet[letter].head = temp;
alfabet[letter].count++;
}
My first question is: is the head now pointing to temp or is it being replaced by temp? Is that causing my problem? I'm using a space reserved for a hash struct and pushing a node inside? This does produce a nice looking hash table but my checker doesn't work. I've made a cursor from a hash struct and from a node struct. Hash works a little bit better. It looks like this:
struct node *cursor = alfabet[teller].head
or
struct hash *cursor = &alfabet[teller]
But it doesn't work. I try to get to the value of a node like this :
with the node cursor : cursor -> value
with the hash cursor : cursor -> head -> value
It does work for a number of words but I run into errors. I get EMs like this.
runtime error: member access within null pointer of type 'struct node'
runtime error: null pointer passed as argument 2, which is declared to never
be null
/usr/include/string.h:145:33: note: nonnull attribute specified here
Segmentation fault
It's the same EM whether I use a node or a hash cursor. The EMs always refer to the line in which I strcmp the word from the text with the value in a node. I can't fix it. I've tried and tried and tried. Maybe I could solve this by dereferencing the value properly but I can't figure out how. I hope I've given you sufficient information. Of course I can give you lots more. Thank you.
hash
struct? Does it mean you're attempting a hashmap (which is just an array of linked lists, with the index being derived from some hash)? – Blauelf Aug 4 '17 at 14:10