title: How can a double pointer be used for a two dimensional matrix?
body: <p>I am trying my hand at C by implementing Conway's game of Life.</p>
  
  <p>I am trying to dynamically build two grids (<code>int</code> matrices), one for the current and one for the next generation, so after I determine what the next generation looks like, I just swap pointers.</p>
  
  <p>At first I tried hopelessly to define the pointer to the grid like <code>int * grid</code>, which you cannot subscript with a second set of brackets like <code>[][]</code> because - obviously - the first set of brackets returns an int.</p>
  
  <p>I also tried something like <code>int * grid[HEIGHT][WIDTH]</code>, but this gives problems assigning one pointer like this to another. (And in fact, I have no idea what this really does in memory!)</p>
  
  <p>In my naïve hopefulness, I thought the following could work after stumbling across double pointers. The program compiles, but fails when running on the line indicated. (In Windows, I get no more detail other than that the Problem Event Name is <code>APPCRASH</code>).</p>
  
  <p><strong>DISCLAIMER:</strong> This is not the actual program, just a proof of concept for the problem.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int HEIGHT = 20;
  int WIDTH = 20;
  
  int ** curr_gen; // Current generation
  int ** next_gen; // Next generation
  
  /* Entry Point main */
  int main(int argc, char** argv) {
  
      // Allocate memory for the grids
      curr_gen = malloc(sizeof (int) * WIDTH * HEIGHT);
      next_gen = malloc(sizeof (int) * WIDTH * HEIGHT);
  
      curr_gen[0][0] = 0; //&lt;&lt; PROGRAM FAILS HERE
  
      // Release heap resources
      free(curr_gen);
      free(next_gen);
  
      return 0;
  }
  </code></pre>