title: Dynamic allocation of array of a struct type
body: <p>I have a problem in adding an element (a struct) in a dynamic array which is in a struct.</p>
  
  <p>Here the main struct</p>
  
  <pre><code>struct testfw_t
  {
      char* program;
      int timeout;
      char *logfile;
      char *cmd;
      bool silent;
      bool verbose;
      struct testTab *tests;
  };
  </code></pre>
  
  <p>Here the array</p>
  
  <pre><code>struct testTab 
  {
      int size;
      struct test_t  *oneTest;        
  };
  </code></pre>
  
  <p>and at last the element to add:</p>
  
  <pre><code>struct test_t
  {
      char *suite;        /**&lt; suite name */
      char *name;         /**&lt; test name */
      testfw_func_t func; /**&lt; test function */
  };
  </code></pre>
  
  <p>So I have to add a <code>struct test_t</code> in the array <code>testTab</code> in the <code>struct testfw_t</code> and I am lost in a lot of <code>malloc</code> and <code>realloc</code> calls.</p>
  
  <p>PS : The init of the main struct if it can be useful which works:</p>
  
  <pre><code>struct testfw_t *testfw_init(char *program, int timeout, char *logfile, char *cmd, bool silent, bool verbose){
  
      struct testfw_t *t;
      t = malloc(sizeof(struct testfw_t));
  
      t-&gt;program = program;
      t-&gt;timeout = timeout;
      t-&gt;logfile = logfile;
      t-&gt;cmd = cmd;
      t-&gt;silent = silent;
      t-&gt;verbose = verbose;
      t-&gt;tests = malloc(sizeof(struct testTab));
      t-&gt;tests-&gt;size=0;
      t-&gt;tests-&gt;oneTest=NULL;
  
      return t;
  }
  </code></pre>
  
  <p>EDIT : What I am trying</p>
  
  <pre><code>struct test_t *nouveau;
  
  nouveau-&gt;suite = suite;
  nouveau-&gt;name = name;
  nouveau-&gt;func=func;
  
  //fw-&gt;tests=realloc(fw-&gt;tests-&gt;oneTest,(fw-&gt;tests-&gt;size+1) * sizeof(struct testTab));
  
  fw-&gt;tests-&gt;oneTest=malloc((fw-&gt;tests-&gt;size+1) * sizeof(nouveau));
  
  fw-&gt;tests-&gt;oneTest[fw-&gt;tests-&gt;size+1] = *nouveau;   
  fw-&gt;tests-&gt;size++;
  
  return nouveau;
  </code></pre>