Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even though the two function arguments can be <em>consumed</em> in the same way, namely via <code>m[i][j]</code>, they're quite different:</p> <ul> <li><p><code>int m[M][N]</code> is an array of <code>M</code> arrays of <code>N</code> ints.</p></li> <li><p><code>int **m</code> is a pointer to a pointer to an int.</p></li> </ul> <p>You cannot pass arrays as function arguments, so an "array of <code>K</code> elements of type <code>T</code>" decays to a "poin&shy;ter-to-<code>T</code>", pointing to the first element of the array. Thus it is permissible and equivalent to write the first form as <code>int m[][N]</code> in a function argument, since the value <code>M</code> is lost. However, the value <code>N</code> is <em>not</em> lost; it is part of the type!</p> <p>So the following are admissible/erroneous for the first form:</p> <pre><code>void f(int arr[M][N]); int a[M][N]; int b[2*M][N]; int c[M][N + 1]; f(a); // OK f(b); // OK; slowest extent is forgotten in the decay //f(c); // Error! 'c' is not an array of {array of N ints}. </code></pre> <p>For the second form, the meaning is rather different:</p> <pre><code>void g(int **p); int a; int * pa = &amp;a; g(&amp;pa); // fine, pointer to 'pa' int arr[M][N]; // g(arr); // Error, makes no sense </code></pre> <p>The expression <code>arr</code> designates the pointer to the first element of an array of arrays of <code>N</code> integers, i.e. its type is <code>int (*)[N]</code>. Dereferencing it gives an array of <code>N</code> integers, and <em>not</em> a pointer to an integer.</p> <p>There is no way to convert the expression <code>arr</code> into a pointer to a pointer: If you said,</p> <pre><code>int ** fool = (int**)arr; </code></pre> <p>then <code>*fool</code> would point to the first element of the first array (<code>arr[0]</code>), and <em>not to an <code>int</code> pointer</em>. So you cannot dereference the value further, because the value <em>is not a pointer</em>.</p> <p>The only <em>correct</em> way to pass a two-dimensional array as a double pointer is to construct an intermediate helper array:</p> <pre><code>int * helper[M]; // array of pointers for (size_t i = 0; i != M; ++i) { helper[i] = arr[i]; // implicit decay } g(helper); // or "g(&amp;helper[0])" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload