Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At a low level, there is no such thing as a multi-dimensional array. There is just a flat block of memory, large enough to hold a given number of elements. In C, a multi-dimensional array is conceptually an array whose elements are also arrays. So if you do:</p> <pre><code>int array[2][3]; </code></pre> <p>Conceptually you end up with:</p> <pre><code>array[0] =&gt; [0, 1, 2] array[1] =&gt; [0, 1, 2] </code></pre> <p>This results in the elements being arranged contiguously in memory, because <code>array[0]</code> and <code>array[1]</code> are not actually holding any data, they are just references to the two inner arrays. Note that this means that only the <code>[0, 1, 2]</code> entries actually occupy space in memory. If you extend this pattern out to the next dimension, you can see that:</p> <pre><code>int array[2][3][2]; </code></pre> <p>...will give you a structure like:</p> <pre><code>array[0] =&gt; [0] =&gt; [0, 1] [1] =&gt; [0, 1] [2] =&gt; [0, 1] array[1] =&gt; [0] =&gt; [0, 1] [1] =&gt; [0, 1] [2] =&gt; [0, 1] </code></pre> <p>Which continues arranging the elements consecutively in memory (as above, only the <code>[0, 1]</code> entries actually occupy space in memory, everything else is just part of a reference to one of these entries). As you can see, this pattern will continue no matter how many dimensions you have.</p> <p>And just for fun:</p> <pre><code>int array[2][3][2][5]; </code></pre> <p>Gives you:</p> <pre><code>array[0] =&gt; [0] =&gt; [0] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0, 1, 2, 3, 4] [2] =&gt; [0] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0, 1, 2, 3, 4] array[1] =&gt; [0] =&gt; [0] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0, 1, 2, 3, 4] [2] =&gt; [0] =&gt; [0, 1, 2, 3, 4] [1] =&gt; [0, 1, 2, 3, 4] </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