title: Initialize a struct that contains pointer to a pointer to a struct
body: <p>I want to initialize a struct that contains different types of variables. For example, suppose I have</p>
  
  <pre><code>struct population {
      int *ids;
      double *incomes;
      struct good **goodsdistn; // This is the one I am having trouble with.
  };
  
  struct population popn;
  </code></pre>
  
  <p>I want to initialize <code>popn</code> using the parameters that are defined in another struct, say</p>
  
  <pre><code>struct params {
      int numpeople;
      // there are other parameters here, not relevant for the question.
  };
  
  struct params parameters = {.numpeople = 50};
  </code></pre>
  
  <p>To initialize <code>popn</code> I am thinking of doing the following:</p>
  
  <p>(1) Define the following function outside  <code>main()</code></p>
  
  <pre><code>void create_population(struct population *popn, struct params *parameters)
  {
      popn -&gt; ids = malloc(sizeof(int) * parameters -&gt; numpeople); //This works
      popn -&gt; incomes = malloc(sizeof(double) * parameters -&gt; numpeople); //This works
      popn -&gt; goodsdistn = malloc(sizeof(???) * parameters -&gt; numpeople);
      // What do I put in place of ??? when I have a pointer to a pointer to struct good.
  
  }
  </code></pre>
  
  <p>(2) Within <code>main()</code> call this function to initialize popn (later I can then fill the struct members):</p>
  
  <pre><code>create_population(&amp;popn, &amp;parameters);
  </code></pre>
  
  <p>Thanks for your help.</p>