4.2 Insert

This is incorrect, do not use this and expect it to work!

The first trial of the insertion code may have logic in listing 3.


Listing 3:hashinsertion
 
1void Hash_insert(struct HashTable *_pht, const char *key, const char *value) 
2{ 
3  struct _HashTable *pht = ((struct _HashTable *)_pht); 
4  int hashvalue = h(key, MAX_ENTRIES); 
5  ++pht->size; 
6  pht->table[hashvalue].flags |= HASHENTRY_INUSE; 
7  strncpy(pht->table[hashvalue].value, 
8          value, 
9          sizeof(pht->table[hashvalue].value)); 
10}

Several aspects of this code are correct. First, the number of values stored should be incremented. Second, we need to mark an entry in the hash table as “used”. Third, we need to copy the value to an entry in the hash table.