title: C Heap Allocating a Hard Coded Struct Array
body: <p>I am having trouble figuring out how to make a hard-coded array heap allocated.</p>
  
  <p>Imagine I have the structures:</p>
  
  <pre><code>struct my_struct
  {
      ...
  };
  
  struct holder
  {
      my_struct *array_of_struct;
      ...
  };
  </code></pre>
  
  <p>Now to create an instance of <code>struct holder</code> though, the array of <code>struct my_struct</code> has to be hard-coded, such as:</p>
  
  <pre><code>struct holder *new_holder()
  {
      struct holder *my_holder = malloc(sizeof(struct holder));
      if (my_holder == NULL)
          exit(-1);
  
      struct my_struct arr[] = {mystruct_instace_1, mystruct_instance_2, ...};
      holder-&gt;array_of_struct = arr;
      return holder;
  }
  </code></pre>
  
  <p>This assignment though wont work, because its pointing to <code>arr</code> which is stack allocated. How would I go about making this assignment of <code>holder-&gt;array_of_struct</code> heap allocated?</p>