News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

C++ Tutorial – 08 – Arrays

An array is a data structure used for storing a collection of values that all have the same data type.

Array declaration and allocation

To declare an array you start as you would a normal variable declaration, but in addition append a set of square brackets following the array’s name. The brackets contain the number of elements in the array. The default values for these elements are the same as for variables – elements in global arrays are initialized to their default values and elements in local arrays remain uninitialized.

int myArray[3]; // integer array with 3 elements

Array assignment

To assign values to the elements you can reference them one at a time by placing the element’s index inside the square brackets, starting with zero.

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

You can also assign values at the same time as the array is declared by enclosing them in curly brackets. The specified array length may be left out to let the array size be decided by the number of values assigned.

int myArray[3] = { 1, 2, 3 };
int myArray[] = { 1, 2, 3 };

Once the array elements are initialized they can be accessed by referencing the index of the element you want.

std::cout << myArray[0]; // "1"

Multi-dimensional arrays

Arrays can be made multi-dimensional by adding more sets of square brackets. As with single-dimensional arrays, they can either be filled in one at a time or all at once during the declaration.

int myArray[2][2] = { { 0, 1 }, { 2, 3 } };
myArray[0][0] = 0;
myArray[0][1] = 1;

Dynamic arrays

Because the arrays above are made up of static (non-dynamic) memory, their size must be determined before execution. Therefore, the size needs to be a constant value. In order to create an array with a size that is not known until run-time you need to use dynamic memory, which is allocated with the new keyword and must be assigned to a pointer or reference.

int* p = new int[3]; // dynamically allocated array

Any array in C++ is actually a pointer to the first element in the array. The referencing of array elements can be made just as well with pointer arithmetic. By incrementing the pointer by one you move to the next element in the array, because changes to a pointer’s address are implicitly multiplied by the size of the pointer’s data type.

*(p+1) = 10; // p[1] = 10;

Determining array size

Keep in mind that just as with any other pointer it is possible to exceed the valid range of an array. It is therefore important to keep track of the array’s length. To determine the length of a statically allocated array you can use the sizeof operator.

int length = sizeof(myArray) / sizeof(int); // 3

This method cannot be used for dynamically allocated arrays. The only way to determine the size of such an array is through the variable used in its allocation.

int size = 3;
int* p = new int[size]; // dynamically allocated array

When you are done using a dynamic array you must remember to delete it. This is done using the delete keyword with an appended set of square brackets.

delete[] p; // release allocated array
Recommended additional reading:
Sams - Teach Yourself C++ in One Hour a Day