title: All the values are the same when I use dynamic allocation
body: <p>I try to get words from user until he enters <code>0</code> and then print it. I want to dynamically allocate a size for the list after every word (by using struct):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct my_struct
  {
      char** words;
      int length;
  } my_struct;
  
  
  int main(void)
  {
      struct my_struct list = { 0,0 };
      list.words = (char**)malloc(0);
      int choice = 1, i = 0;
      char str[30];
      printf("Enter 0 to exit or any other number to continue: ");
      scanf("%d", &amp;choice);
      while (choice)
      {
          list.length += 1;
          list.words = (char**)realloc(list.words, sizeof(char*) * list.length);
          printf("Enter a word: ");
          getchar();
          fgets(str, 30, stdin);
          list.words[list.length - 1] = (char*)malloc(sizeof(char) * 30);
          list.words[list.length - 1] = str;
          printf("Enter 0 to exit or any other number to continue: ");
          scanf("%d", &amp;choice);
      }
      printf("Values:\n");
      for (i = 0; i &lt; list.length; i++)
      {
          printf("%s", list.words[i]);
      }
      free(list.words);
      getchar();
      getchar();
      return 0;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ycAqJ.png" rel="nofollow noreferrer">output</a>
  Why does it happen?</p>