Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two varieties of 2D array in C, though the syntax for accessing either is confusingly similar. One variety is to have a contiguous range of memory holding all the array values with the compiler 'folding' the memory into an apparent multi-dimensional shape for you:</p> <pre><code>int contiguous2DArray[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int another2DArray[4][3] = { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }; int a3DArray[2][2][3] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; /* A series of true statements. */ contiguous2DArray[0][0] == 0; contiguous2DArray[0][1] == 1; contiguous2DArray[1][0] == 4; another2DArray[0][0] == 12; another2DArray[0][1] == 13; another2DArray[1][0] == 15; a3DArray[0][0][0] == 0; a3DArray[0][0][1] == 1; a3DArray[0][1][0] == 3; a3DArray[1][0][0] == 6; </code></pre> <p>All but the very last array size are a required part of the type. Since these sizes are known at compile time the compiler knows to skip along the required amount when incrementing indexes. The last index always causes the compiler to skip along one space every time it goes up by one. The second the last will skip along by <code>n</code> where n is the last size, the third to the last will skip along by <code>m * n</code> where m and n are the last two sizes.</p> <p>As you can see, since the sizes are not able to vary at run time, this sort of array isn't a great fit for one all dimensions can vary at run time.</p> <p>For arrays like this, you need to start using arrays of pointers to arrays instead of multidimensional arrays of contiguous memory.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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