Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each pair of square brackets is a dereferencing operation (when applied to a pointer). As an example, the following pairs of lines of code are equivalent:</p> <pre><code>x = myArray[4]; x = *(myArray+4); </code></pre> <p>&nbsp;</p> <pre><code>x = myArray[2][7]; x = *((*(myArray+2))+7); </code></pre> <p>To use your suggested syntax you are simply dereferencing the value returned from the first dereference.</p> <pre><code>int*** myArray = (some allocation method, keep reading); // // All in one line: int value = myArray[x][y][z]; // // Separated to multiple steps: int** deref1 = myArray[x]; int* deref2 = deref1[y]; int value = deref2[z]; </code></pre> <p>To go about allocating this array, you simply need to recognise that you don't actually have a three-dimensional array of integers. You have an array of arrays of arrays of integers.</p> <pre><code>// Start by allocating an array for array of arrays int*** myArray = new int**[X_MAXIMUM]; // Allocate an array for each element of the first array for(int x = 0; x &lt; X_MAXIMUM; ++x) { myArray[x] = new int*[Y_MAXIMUM]; // Allocate an array of integers for each element of this array for(int y = 0; y &lt; Y_MAXIMUM; ++y) { myArray[x][y] = new int[Z_MAXIMUM]; // Specify an initial value (if desired) for(int z = 0; z &lt; Z_MAXIMUM; ++z) { myArray[x][y][z] = -1; } } } </code></pre> <p>Deallocating this array follows a similar process to allocating it:</p> <pre><code>for(int x = 0; x &lt; X_MAXIMUM; ++x) { for(int y = 0; y &lt; Y_MAXIMUM; ++y) { delete[] myArray[x][y]; } delete[] myArray[x]; } delete[] myArray; </code></pre>
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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