title: How to insert a structure instance inside a structure type array in c?
body: <h1>What do I want to do?</h1>
  <p>I want to make an array of <code>struct</code> types and insert <code>struct</code> instances into it.</p>
  <pre class="lang-c prettyprint-override"><code>{&quot;struct Matrix matrix1&quot;, &quot;struct Matrix matrix2&quot;}
  </code></pre>
  <hr />
  <h1>What have I done till now?</h1>
  <p>I have created a function <code>create_struct()</code> that takes the necessary parameters for the <code>struct</code> and return a <code>struct</code> pointer.</p>
  <pre class="lang-c prettyprint-override"><code>typedef struct
  {
      size_t rows, columns;
      int *table;
  } mat;
  
  mat *create_struct(int x, int y)
  {
      mat *data = (mat *)malloc(sizeof(*data));
      data-&gt;table = malloc(sizeof(int[(x * y)]));
      data-&gt;columns = y;
      data-&gt;rows = x;
      return data;
  }
  </code></pre>
  <p>Then I created a pointer-to-object in this case <code>struct</code> and tried inserting <code>struct</code> instances with help of a for loop and the <code>create_struct()</code> function. But doing so the program crashes.</p>
  <pre class="lang-c prettyprint-override"><code>int main()
  {
      size_t dimensions[2][2] = {{3, 3}, {3, 2}};
  
      mat *matrix;
  
      for (size_t i = 0; i &lt; 2; i++)
      {
          matrix[i] = create_struct(dimensions[i][0], dimensions[i][1]);
      }
  
      return 0;
  }
  </code></pre>
  <p>I think the main issue is with this line.</p>
  <pre class="lang-c prettyprint-override"><code>matrix[i] = create_struct(dimensions[i][0], dimensions[i][1]);
  </code></pre>
  <p>First of all, the above will result in to compile error <code>a value of type &quot;mat *&quot; cannot be assigned to an entity of type &quot;mat&quot;</code></p>
  <p>So to solve this I did something like this. <strong>Which I think is completely vague.</strong> I might be wrong.</p>
  <pre class="lang-c prettyprint-override"><code>matrix[i] = *create_struct(dimensions[i][0], dimensions[i][1]);
  </code></pre>
  <hr />
  <h1>What do I expect from the answers</h1>
  <p>The following should be the main point of focus:</p>
  <ul>
  <li>Is it possible to do something like I'm doing?</li>
  <li>Is there a better way or procedure to insert <code>struct</code> instances into a <code>struct</code> types array?</li>
  <li>Provide information if you feel that I lack any conceptual knowledge.</li>
  </ul>