title: 2 dim array and double pointer
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array">Create a pointer to two-dimensional array</a>  </p>
  </blockquote>
  
  
  
  <p>When I calling the functions func4() and func5() i get the followinfg errors:</p>
  
  <blockquote>
    <p>func4() error: cannot convert ‘short int (*)[3]’ to ‘short int**’ for
    argument ‘1’ to ‘int func4(short int**)’| func5() error: cannot
    convert ‘short int (*)[3]’ to ‘short int**’ for argument ‘1’ to ‘int
    func5(short int**)’|</p>
  </blockquote>
  
  <p>How can I correct the error on calling functions func4() and func5()? Here is my code:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  
  int func1(short mat[][3]);
  int func2(short (*mat)[3]);
  int func3(short *mat);
  int func4(short **mat);
  int func5(short *mat[3]);
  
  int main()
  {
  
  short mat[3][3],i,j;
  
  for(i = 0 ; i &lt; 3 ; i++)
      for(j = 0 ; j &lt; 3 ; j++)
      {
          mat[i][j] = i*10 + j;
      }
  
  printf(" Initialized data to: ");
  for(i = 0 ; i &lt; 3 ; i++)
  {
      printf("\n");
      for(j = 0 ; j &lt; 3 ; j++)
      {
          printf("%5.2d", mat[i][j]);
      }
  }
  
  printf("\n");
  
  func1(mat);
  func2(mat);
  func3(&amp;mat[0][0]);
  func4(mat); //error: cannot convert ‘short int (*)[3]’ to 
              //‘short int**’ for argument ‘1’ to       ‘int func4(short int**)’|
  func5(mat); //error: cannot convert ‘short int (*)[3]’ to 
              //‘short int**’ for argument ‘1’ to ‘int func5(short int**)’|
  
  return 0;
  }
  
  
  
  /*
  Method #1 (No tricks, just an array with empty first dimension)
  ===============================================================
  You don't have to specify the first dimension!
  */
  
  int func1(short mat[][3])
  {
  register short i, j;
  
  printf(" Declare as matrix, explicitly specify second dimension: ");
  for(i = 0 ; i &lt; 3 ; i++)
  {
      printf("\n");
      for(j = 0 ; j &lt; 3 ; j++)
      {
          printf("%5.2d", mat[i][j]);
      }
  }
  printf("\n");
  
  return 0;
  }
  
  /*
  Method #2 (pointer to array, second dimension is explicitly specified)
  ======================================================================
  */
  
  int func2(short (*mat)[3])
  {
  register short i, j;
  
  printf(" Declare as pointer to column, explicitly specify 2nd dim: ");
  for(i = 0 ; i &lt; 3 ; i++)
  {
      printf("\n");
      for(j = 0 ; j &lt; 3 ; j++)
      {
          printf("%5.2d", mat[i][j]);
      }
  }
  printf("\n");
  
  return 0;
  }
  
  /*
  Method #3 (Using a single pointer, the array is "flattened")
  ============================================================
  With this method you can create general-purpose routines.
  The dimensions doesn't appear in any declaration, so you
  can add them to the formal argument list.
  
  The manual array indexing will probably slow down execution.
  */
  
  int func3(short *mat)
  {
  register short i, j;
  
  printf(" Declare as single-pointer, manual offset computation: ");
  for(i = 0 ; i &lt; 3 ; i++)
  {
      printf("\n");
      for(j = 0 ; j &lt; 3 ; j++)
      {
          printf("%5.2d", *(mat + 3*i + j));
      }
  }
  printf("\n");
  
  return 0;
  }
  
  /*
  Method #4 (double pointer, using an auxiliary array of pointers)
  ================================================================
  With this method you can create general-purpose routines,
  if you allocate "index" at run-time.
  
  Add the dimensions to the formal argument list.
  */
  
  int func4(short **mat)
  {
  short    i, j, *index[3];
  
  for (i = 0 ; i &lt; 3 ; i++)
      index[i] = (short *)mat + 3*i;
  
  printf(" Declare as double-pointer, use auxiliary pointer array: ");
  for(i = 0 ; i &lt; 3 ; i++)
  {
      printf("\n");
      for(j = 0 ; j &lt; 3 ; j++)
      {
          printf("%5.2d", index[i][j]);
      }
  }
  printf("\n");
  
  return 0;
  }
  
  /*
  Method #5 (single pointer, using an auxiliary array of pointers)
  ================================================================
  */
  
  int func5(short *mat[3])
  {
  short i, j, *index[3];
  for (i = 0 ; i &lt; 3 ; i++)
      index[i] = (short *)mat + 3*i;
  
  printf(" Declare as single-pointer, use auxiliary pointer array: ");
  for(i = 0 ; i &lt; 3 ; i++)
  {
      printf("\n");
      for(j = 0 ; j &lt; 3 ; j++)
      {
          printf("%5.2d", index[i][j]);
      }
  }
  printf("\n");
  return 0;
  }
  </code></pre>