title: dynamically resizing an array of strings
body: <p>I need an array of strings in which the length of the array is not known at compile time. what I've done is:</p>
  
  <pre><code>char **words;
  
  words = malloc(capacity * sizeof(char *));
  
  char nextword[MAX_LEN + 1];
  
  while (true) {
      if (num_entries == capacity) {
          capacity += 5;
          realloc(words, (sizeof(char *) * capacity));
      }
  
      printf("\nEnter a word: ");
      fgets (nextword, MAX_LEN, stdin);
  
      remove_newline(nextword);
  
      if (strlen(nextword) == 0) break;
  
      words[num_entries] = malloc(strlen(nextword + 1));
  
      if (words[num_entries] == NULL) {
          printf("\nout of space\n");
          break;
      }
  
      strcpy(words[num_entries], nextword);
      num_entries++;
  </code></pre>
  
  <p>this seems to work to expand the size once, except that the first element after the expansion has become <code>NULL</code> for some reason. The second time <code>realloc</code> is executed I get an error: </p>
  
  <blockquote>
    <p><code>"invalid next size"</code>. </p>
  </blockquote>