title: dynamically allocate an **pointer in a struct
body: <p>I'm trying to learn how to allocate a 2d array within a struct, but it keeps getting a segfault. I know how to allocate a 2d array outside of a struct but i don't know why its getting a segfault when i move it into a struct.</p>
  
  <pre><code>/*program to allocate a 2d array and fill it with 0's and then print it.*/
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  typedef struct stuff
  {
     int **arr;
  }stuff;
  
  int main()
  {
    stuff x;
    int i,j;//loop counters
    //allocate 2d array
    x.arr = (int**)malloc(sizeof(int*)*4);
    for(i=0; i&lt;4; i++)
      x.arr[i] = (int*)malloc(sizeof(int)*4);
  //intialize 2d array to 0
    for (i = 0; i &lt; 4; i++)
       for (j = 0; j &lt; 4; j++)
         x.arr[i][j] = 0;
  //print 2d array
    for(i=0; i&lt;4; i++);
      for(j=0; j&lt; 4; j++)
         printf("arr[%d][%d] = %d \n", i ,j ,x.arr[i][j]);
    return 0;
  }
  </code></pre>