Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A 2D array in C is treated as a 1D array whose elements are 1D arrays (the rows).<br> For example, a 4x3 array of <code>T</code> (where "T" is some data type) may be declared by: <code>T a[4][3]</code>, and described by the following scheme: </p> <pre><code> +-----+-----+-----+ a == a[0] ---&gt; | a00 | a01 | a02 | +-----+-----+-----+ +-----+-----+-----+ a[1] ---&gt; | a10 | a11 | a12 | +-----+-----+-----+ +-----+-----+-----+ a[2] ---&gt; | a20 | a21 | a22 | +-----+-----+-----+ +-----+-----+-----+ a[3] ---&gt; | a30 | a31 | a32 | +-----+-----+-----+ </code></pre> <p>Also the array elements are stored in memory row after row.<br> Prepending the <code>T</code> and appending the <code>[3]</code> to <code>a</code> we have an array of <code>3</code> elements of type <code>T</code>. But, the name <code>a[4]</code> is itself an array indicating that there are <code>4</code> elements each being an array of <code>3</code> elements. Hence we have an array of <code>4</code> arrays of <code>3</code> elements each.<br> Now it is clear that <code>a</code> points to the first element (<code>a[0]</code>) of <code>a[4]</code> . On the Other hand <code>&amp;a[0]</code> will give the address of first element (<code>a[0]</code>) of <code>a[4]</code> and <code>&amp;a[0][0]</code> will give the address of <code>0th</code> row (<code>a00 | a01 | a02)</code> of array <code>a[4][3]</code>. <code>&amp;a</code> will give the address of 2D array <code>a[3][4]</code>. <code>*a</code> decays to pointers to <code>a[0][0]</code>.<br> Note that <code>a</code> is not a pointer to <code>a[0][0]</code>; instead it is a pointer to <code>a[0]</code>.<br> Hence </p> <blockquote> <ul> <li>G1: <code>a</code> and <code>&amp;a[0]</code> are equivalent. </li> <li>G2: <code>*a</code>, <code>a[0]</code>and <code>&amp;a[0][0]</code> are equivalent.</li> <li>G3: <code>&amp;a</code> (gives the address of 2D array <code>a[3][4]</code>).<br> But group <strong><em>G1</em>, <em>G2</em> and <em>G3</em> are not identical although they are giving the same result</strong> (and I explained above why it is giving same result).</li> </ul> </blockquote>
 

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