title: How can I create a dynamically sized array of structs?
body: <p>I know how to create an array of structs but with a predefined size. However is there a way to create a dynamic array of structs such that the array could get bigger?</p>
  
  <p>For example:</p>
  
  <pre><code>    typedef struct
      {
          char *str;
      } words;
  
      main()
      {
          words x[100]; // I do not want to use this, I want to dynamic increase the size of the array as data comes in.
      }
  </code></pre>
  
  <p>Is this possible?</p>
  
  <hr>
  
  <p>I've researched this: <code>words* array = (words*)malloc(sizeof(words) * 100);</code></p>
  
  <p>I want to get rid of the 100 and store the data as it comes in. Thus if 76 fields of data comes in, I want to store 76 and not 100. I'm assuming that I don't know how much data is coming into my program. In the struct I defined above I could create the first "index" as:</p>
  
  <pre><code>    words* array = (words*)malloc(sizeof(words));
  </code></pre>
  
  <p>However I want to dynamically add elements to the array after. I hope I described the problem area clearly enough. The major challenge is to dynamically add a second field, at least that is the challenge for the moment.</p>
  
  <hr>
  
  <p>I've made a little progress however:</p>
  
  <pre><code>    typedef struct {
          char *str;
      } words;
  
      // Allocate first string.
      words x = (words) malloc(sizeof(words));
      x[0].str = "john";
  
      // Allocate second string.
      x=(words*) realloc(x, sizeof(words));
      x[1].FirstName = "bob";
  
      // printf second string.
      printf("%s", x[1].str); --&gt; This is working, it's printing out bob.
  
      free(x); // Free up memory.
  
      printf("%s", x[1].str); --&gt; Not working since its still printing out BOB even though I freed up memory. What is wrong?
  </code></pre>
  
  <p>I did some error checking and this is what I found. If after I free up memory for x I add the following:</p>
  
  <pre><code>    x=NULL;
  </code></pre>
  
  <p>then if I try to print x I get an error which is what I want. So is it that the free function is not working, at least on my compiler? I'm using DevC??</p>
  
  <hr>
  
  <p>Thanks, I understand now due to:</p>
  
  <blockquote>
    <p>FirstName is a pointer to an array of char which is not being allocated by the malloc, only the pointer is being allocated and after you call free, it doesn't erase the memory, it just marks it as available on the heap to be over written later. – MattSmith</p>
  </blockquote>
  
  <h2>Update</h2>
  
  <p>I'm trying to modularize and put the creation of my array of structs in a function but nothing seems to work. I'm trying something very simple and I don't know what else to do. It's along the same lines as before, just another function, loaddata that is loading the data and outside the method I need to do some printing. How can I make it work? My code is as follows:</p>
  
  <pre><code>    # include &lt;stdio.h&gt;
      # include &lt;stdlib.h&gt;
      # include &lt;string.h&gt;
      # include &lt;ctype.h&gt;
  
      typedef struct
      {
          char *str1;
          char *str2;
      } words;
  
      void LoadData(words *, int *);
  
      main()
      {
          words *x;
          int num;
  
          LoadData(&amp;x, &amp;num);
  
          printf("%s %s", x[0].str1, x[0].str2);
          printf("%s %s", x[1].str1, x[1].str2);
  
          getch();
      }//
  
      void LoadData(words *x, int * num)
      {
          x = (words*) malloc(sizeof(words));
  
          x[0].str1 = "johnnie\0";
          x[0].str2 = "krapson\0";
  
          x = (words*) realloc(x, sizeof(words)*2);
          x[1].str1 = "bob\0";
          x[1].str2 = "marley\0";
  
          *num=*num+1;
      }//
  </code></pre>
  
  <p>This simple test code is crashing and I have no idea why. Where is the bug?</p>