title: C - Allocating memory and copying string in to an array for hash table
body: <p>I'm trying to create a hash table struct which contains an array of keys (strings) and an array of frequencies for each time the key appears. The code i'm running looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "mylib.h"
  #include "htable.h"
  
  int main(void){
      htable h = htable_new(18143);
      char word[256];
      while(getword(word, sizeof word, stdin) !=EOF){
          htable_insert(h, word);
      }
  
      htable_print(h);
      htable_free(h);
  
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Which creates a new hash table, reads in and stores words, then prints. E.g if the input was "one" "two" "three" the output would look like:</p>
  
  <pre><code>1    one
  1    two
  1    three
  </code></pre>
  
  <p>Where the left column is frequency and the right is the key. Below is the actual hash table code.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include "mylib.h"
  #include "htable.h"
  
  struct htablerec{
      int capacity;
      int num_keys;
      int *frequencies;
      char *keys;
  };
  
  
  htable htable_new(int n){
          int i;
          htable result = emalloc(sizeof *result);
          result-&gt;capacity = n;
          result-&gt;num_keys = 0;
          result-&gt;frequencies = emalloc(result-&gt;capacity * sizeof result-&gt;frequencies[0]);
          result-&gt;keys = emalloc(result-&gt;capacity * sizeof result-&gt;keys[0]);
          for(i=0;i&lt;result-&gt;capacity;i++){
              result-&gt;frequencies[i] = 0;
              result-&gt;keys[i] = '\0';
          }
          return result;  
  }
  
  static unsigned int htable_word_to_int(char *word){
      unsigned int result = 0;
      while(*word != '\0'){
          result = (*word++ + 31 * result);
      }
      return result;
  }
  
  
  int htable_insert(htable h, char *str){
      unsigned int key = htable_word_to_int(str);
      unsigned int initial_index = (key % h-&gt;capacity);
  
  
      if(h-&gt;keys[initial_index] == '\0'){
              h-&gt;keys[initial_index] = emalloc(strlen(str)+1 * sizeof str[0]);
              strcpy(h-&gt;keys[initial_index], str);
              h-&gt;frequencies[initial_index] = 1;
              h-&gt;num_keys++;
              return 1;
          }
  
      else if(h-&gt;keys[initial_index] == *str){
              h-&gt;frequencies[initial_index]++;
              return h-&gt;frequencies[initial_index];
          }
      return 0;
      }
  
  void htable_print(htable h){
      int i;    
      for(i=0;i&lt;h-&gt;capacity;i++){
          if(h-&gt;frequencies[i] &gt;0){
              printf("%d  %s\n", h-&gt;frequencies[i], h-&gt;keys[i]);
      }
  }
  
  }
  
  void htable_free(htable h){
      free(h-&gt;frequencies);
      free(h-&gt;keys);
      free(h);
  }
  </code></pre>
  
  <p>Basically the insert function takes an htable and a string. It converts the string into an integer and divides to get an index within the size of the key array of the htable. If the index is null, there's nothing there so allocate enough memory and insert the string, or if the thing there is the same string increment the frequency. Errors are being thrown over:</p>
  
  <pre><code>assignment makes integer from pointer without a cast [-Wint-conversion]
  h-&gt;keys[initial_index] = emalloc(strlen(str)+1 * sizeof str[0]);
                             ^
  htable.c:44:11: warning: passing argument 1 of ‘strcpy’ makes pointer   from integer without a cast [-Wint-conversion]
  strcpy(h-&gt;keys[initial_index], str);
  </code></pre>
  
  <p>The emalloc function in question:</p>
  
  <pre><code>void *emalloc(size_t s){
      void *result = malloc(s);
      if(NULL == result){
          fprintf(stderr, "Memory allocation error");
          exit(EXIT_FAILURE);
      }
      return result;
  }
  </code></pre>
  
  <p>It's also causing an error with the printing as the %s argument has type int. I'm still getting used to pointers in c and i'm sure that is the issue here based on the errors.</p>