Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The syntax of your second example is the syntax for a multidimensional array literal. </p> <p>A multidimensional array is not an array of pointers to arrays.</p> <p>It would be surprising if the syntax for one thing was also the syntax for a different thing, depending on the type it is declared to be. </p> <p>In the first example, because a string literal is evaluated to a pointer rather than an array value, the value is evaluated as an array of pointers. Because an array literal is evaluated as an array value rather than a pointer, the second example is a multidimensional array - an array whose elements are array values, not an array whose elements are pointers to array values. </p> <p>The following code demonstrates the combinations of multidimensional arrays, pointers to arrays, arrays of pointers, and pointers to pointers. Of these three, only arrays of pointers and pointers to pointers are compatible with each other:</p> <pre><code>void multi_array (int x[][4], size_t len) // multidimensional array { for (size_t i = 0; i &lt; len; ++i) for (size_t j = 0; j &lt; 4; ++j) putchar( 'a' + x[i][j] ); putchar('\n'); } void ptr_array (int (*x)[4], size_t len) // pointer to an array { ... as multi_array } void array_ptr (int *x[], size_t len) // array of pointers { ... as multi_array } void ptr_ptr (int **x, size_t len) // pointer to pointer { ... as multi_array } int main() { int a[][4] = { { 1,2,3,4 } }; int b[] = { 1,2,3,4 }; int* c[] = { b }; multi_array( a, 1 ); multi_array( (int[][4]) { { 1,2,3,4} }, 1 ); // literal syntax as value ptr_array( &amp;b, 1 ); array_ptr( c, 1 ); ptr_ptr( c, 1 ); // only last two are the same return 0; } </code></pre>
 

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