CHAPTER 7

image

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;

Alternatively, you can assign values at the same time as the array is declared by enclosing them in curly brackets. The specified array length may optionally 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;

The extra curly brackets are optional, but including them is good practice since it makes the code easier to understand.

int mArray[2][2] = { 0, 1, 2, 3 }; // alternative

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

An array in C++ behaves as a constant pointer to the first element in the array. The referencing of array elements can therefore 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;

Array Size

Just as with any other pointer, it is possible to exceed the valid range of an array and thereby rewrite some adjacent memory. This should always be avoided since it can lead to unexpected results or crash the program.

int myArray[2] = { 1, 2 };
myArray[2] = 3; // out of bounds error

To determine the length of a regular (statically allocated) array, the sizeof operator can be used.

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

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.26.217