title: Hashmap implementation problem in C with void pointer as value
body: <p>Hi I am attempting to implement a really simple hashmap in regular C with a string as key and a void pointer as value as I wish to use the map for multiple data types.</p>
  
  <p>So far I have this</p>
  
  <pre><code>struct node{
      void * value;
      char * key;
  };
  
  unsigned long strhash(char *string)
  {   
      unsigned long hash = 5381;
      int c;
  
      while ((c = *string++))
      {   
          hash = ((hash &lt;&lt; 5) + hash) + c;
      }   
      return hash;
  }
  
  
  map_t *map_create(int maxSize){
  
      map_t *map = malloc(sizeof(map_t));
      map-&gt;curSize = 0;
      map-&gt;maxSize = maxSize;
      map-&gt;nodes = calloc(map-&gt;maxSize, sizeof(node_t *));
  
      return map;
  }
  
  
  node_t *node_create(char *key, void *value){
  
      node_t *node = malloc(sizeof(node_t));
      node-&gt;key = key;
      node-&gt;value = value;
      return node;
  }
  
  void map_insert(map_t *map, char *key, void *value){
  
      node_t *node = node_create(key, value);
  
      int idx = strhash(key) % map-&gt;maxSize;
      if(map-&gt;nodes[idx] == NULL){
          map-&gt;nodes[idx] = node;
      }else{
          while(map-&gt;nodes[idx] != NULL){
              idx++%map-&gt;maxSize;
          }
          map-&gt;nodes[idx] = node;
      }   
      return;
  }
  
  void map_print(map_t *map){
  
      for(int i = 0; i &lt; map-&gt;maxSize; i++){
          if(map-&gt;nodes[i] != NULL){
              printf("index: %d\t value: %d\n",i, *(int*)map-&gt;nodes[i]-&gt;value);
          }
      }
      return;
  }
  
  void map_destroy(map_t *map){
       for(int i = 0; i &lt; map-&gt;maxSize; i++){
          if(map-&gt;nodes[i] != NULL){
              free(map-&gt;nodes[i]);
          }
      }
      free(map-&gt;nodes);
      free(map);
      return;
  }
  
  
  
  int main(){
  
      map_t *map = map_create(32);
      for(int i = 0; i &lt; 30; i++){
          map_insert(map, (char*)&amp;i, &amp;i);
      }
      map_print(map);
      map_destroy(map);
      return 0;
  }
  </code></pre>
  
  <p>The problem is the output is not as I'd expect when the map gets printed all that is retrieved is the value "30" on all indexes which is the last number inserted into the map. If I change the value to type int the map works as expected, so is there must be something crucial I am missing in regards to pointers. </p>
  
  <p>I am not the greatest at C so any light which could be shed on this would be most appreciated.</p>