C++ arrays

C++ arrays are somewhat different from Java arrays. There are arrays declared statically and arrays declared dynamically. All arrays are references. The value of an array is its address. As in Java, arrays subscripts start at zero. In C++, arrays do not know how many elements they have.

Statically declared arrays
Statically declared arrays are allocated memory at compile time and their size is fixed, i.e., cannot be changed later. They can be initialized in a manner similar to Java.

For example two int arrays are declared, one initialized, one not.
   int a[10];
   int b[5] {8, 20, 25, 9, 14};
The   a   array has 10 elements with subscripts numbered from 0 to 9, filled with garbage. The   b   array has 5 elements with subscripts numbered from 0 to 4, filled with the five given values. They accessed in the usual way, e.g., a[5] or b[2]. The memory is allocated at compile time. A memory picture of these arrays show 10 int garbage values and 5 valid int values:
     __     ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ 
   a| -|-->|...|...|...|...|...|...|...|...|...|...|
     --     --- --- --- --- --- --- --- --- --- --- 
             0   1   2   3   4   5   6   7   8   9
     __     ___ ___ ___ ___ ___ 
   b| -|-->| 8 | 20| 25| 9 | 14|
     --     --- --- --- --- --- 
Static multi-dimensional arrays are declared with multiple dimensions. For example, a 2-dimensional array, a, has 3 rows and 4 columns:
   int a[3][4];
The memory picture that will help you to solve problems is shown below. In reality, memory is contiguous, so this two-dimensional array is really stored as one long one-dimensional array. It is stored in what's called row-order, meaning by row. In memory, the second row follows the first row, and the third row follows the second row.
     __       ___ ___ ___ ___
   a| -|--> 0|...|...|...|...|
     --       --- --- --- ---
            1|...|...|...|...|
              --- --- --- ---
            2|...|...|...|...|
              --- --- --- ---
               0   1   2   3
Dynamically declared arrays
If you want to be able to alter the size of your array at run time, then declare dynamic arrays. These are done with pointers and the new operator. For the basics on pointers, read the pointers section.

Allocate memory using new, and then you access the array in the same way you would a static array. For example,
   int* arrayPtr = new int[10];
   for (int i = 0; i < 10; i++) {
      arrayPtr[i] = i;
   }
The memory picture is identical to the static array, but you can change the size if you need to. Don't forget you must deallocate the memory before allocating new memory (or you will have a memory leak).
   delete [] arrayPtr;      // the [] is needed when deleting array pointers
   arrayPtr = new int[50];
   . . .
When you're completely done with the array, you must delete its memory:
   delete [] arrayPtr;
Dynamic multi-dimensional arrays are done in a similar manner to Java. You will have pointers to pointers. For an example, see a