Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Multidimensional arrays in C are arrays of arrays. The elements of an n-dimensional array are (n-1) dimensional arrays. <code>A[0]</code>, for example, is an <code>int [3]</code>.</p> <pre> A: |24|25|26|44|45|46| A[0]: |24|25|26| A[1]: |44|45|46| </pre> <p>In certain contexts, an array is converted to a pointer to the first element of the array, but the array is not itself a pointer.</p> <p>From the C standard, § 6.5.2.1-3:</p> <blockquote> <p>3 Successive subscript operators designate an element of a multidimensional array object. If <em>E</em> is an n-dimensional array (n≥2) with dimensions i× j×...×k, then <em>E</em> (used as other than an lvalue) is converted to a pointer to an (n − 1)-dimensional array with dimensions j × . . . × k. If the unary * operator is applied to this pointer explicitly, or implicitly as a result of subscripting, the result is the pointed-to (n − 1)-dimensional array, which itself is converted into a pointer if used as other than an lvalue. It follows from this that arrays are stored in row-major order (last subscript varies fastest).</p> <p>4 EXAMPLE Consider the array object defined by the declaration</p> <pre><code>int x[3][5]; </code></pre> <p>Here x is a 3 × 5 array of ints; more precisely, x is an array of three element objects, each of which is an array of five ints. In the expression <code>x[i]</code>, which is equivalent to <code>(*((x)+(i)))</code>, x is first converted to a pointer to the initial array of five ints. Then i is adjusted according to the type of x, which conceptually entails multiplying i by the size of the object to which the pointer points, namely an array of five int objects. The results are added and indirection is applied to yield an array of five ints. When used in the expression <code>x[i][j]</code>, that array is in turn converted to a pointer to the first of the ints, so <code>x[i][j]</code> yields an int.</p> </blockquote> <p>To create a <code>ptr_array</code> as diagrammed:</p> <pre><code>int (*ptr_array[2])[3] ptr_array[0] = A; ptr_array[1] = A+1; // or: ptr_array[0] = &amp;A[0]; ptr_array[1] = &amp;A[1]; // or even: ptr_array[0] = (int(*)[3])A[0]; ptr_array[1] = (int(*)[3])A[1]; // though this last shouldn't be used in production code </code></pre> <p>Here's how to work out the declaration. From the diagram, ptr_array is an array of size 2. </p> <pre><code>... ptr_array[2] ... </code></pre> <p>The elements of the array are pointers</p> <pre><code>*ptr_array[2] </code></pre> <p>to arrays of size 3</p> <pre><code>(*ptr_array[2])[3] </code></pre> <p>of ints</p> <pre><code>int (*ptr_array[2])[3] </code></pre> <p>If you're ever not certain how to declare something like this, you can use <a href="http://cdecl.org/" rel="nofollow">cdecl</a>:</p> <pre> cdecl> declare ptr_array as array 2 of pointer to array 3 of int int (*ptr_array[2])[3] </pre> <p>You can install a command line version of cdecl (if not already installed) on your development computer. The exact method depends on the platform. Check the documentation and web at large.</p>
 

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