I am trying to create a hash table in C from scratch. Here is a hash table with 1 byte (char*
) keys and values I want to make, except I want my hash table to store keys and values as strings up to 32 characters long (char key[32], char value[32]
). Here is my struct
:
#define KV_SIZE 32
typedef struct hash_entry{
char key[KV_SIZE];
char value[KV_SIZE];
struct hash_entry* next;
} hash_entry;
I am having trouble forming a function called create_entry()
because I don't know how to assign my struct
strings, key and value, to values.
// create an entry
hash_entry* create_entry(char key[KV_SIZE], char value[KV_SIZE]){
printf("%s\n", key);
hash_entry* entry = (hash_entry*)malloc(sizeof(hash_entry*));
// I want entry->key and entry->value to store a string up to 32 chars long
strncpy(entry->key, key, strlen(key)); // Error
strncpy(entry->value, value, strlen(value)); // Error
entry->next = NULL;
return entry;
}
So far, it seems like I need my entry
's to remain declared as pointers (hash_entry* entry
) and not non-pointers (hash_entry entry
) to be able to link them later.