title: 2D array initialisation in C
body: <p>I know this is an old chestnut, but I want a small 2D array statically allocated in my code. I know the way to do this is:</p>
  
  <pre><code>static int A[3][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
  </code></pre>
  
  <p>That's fine and I can access all the members of it. However I have several problems passing it to a function, e.g.:</p>
  
  <pre><code>void print_matrix(int **a, int r, int c)
  {
      int x, y;
  
      for(x = 0; x &lt; r; x++)
      {
          printf("Row %02d = %#x = ", x, a[x]);
  
          for(y = 0; y &lt; c; y++)
          {
              printf("%s%d", (0 == y) ? "" : ", ", a[x][y]);
          }
          printf("\n");
      }
  }
  </code></pre>
  
  <p>Firstly I can't simply pass <code>A</code> to the function, I need to cast it to (int **). Since <code>char *</code> is synonymous to <code>char []</code>, I was a little surprised at this. Secondly, it crashes and when I check in the debugger, within the sub-function, <code>a[0]</code> is reported as <code>1</code> and not a pointer to an array of integers.</p>
  
  <p>I know there is compiler/C language arcane magic happening here. But it is all a little confusing. If I try to initialise as:</p>
  
  <pre><code>static int *A[3] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
  </code></pre>
  
  <p>I get a ton of warnings. How does this differ to:</p>
  
  <pre><code>static char *S[3] = { "hello", "there", "stackoverflow" };
  </code></pre>
  
  <hr>
  
  <p>Apart from the question of arcane C magic, which somehow I have never learnt despite over a decade of C programming :(, I would like to know how to generate my array so I can successfully pass it as an <code>int **</code> without having to go through all the fag of for loops or copying the statically allocated array to a dynamically allocated one.</p>
  
  <p>Would the following work?</p>
  
  <pre><code>int *A0 = { 1, 2 };
  int *A1 = { 3, 4 };
  int *A2 = { 5, 6 };
  int **A = { A0, A1, A2 };
  </code></pre>
  
  <p>Is there a nicer way than this of doing it?</p>
  
  <p>Thanks, all.</p>
  
  <p>P.s. I know that in real life we would read values from a DB or file into dynamically allocated arrays and avoid all this stuff.</p>