C++ Arrays

Array is a collection of similar Objects

We can have array of Integers, Characters, Strings, any user defined types, etc. Since we can have any kind of collection (Integers, Characters, Strings, etc.) in an array, so in a generic way we can call array is a collection of similar objects.

Arrays are fixed in size

After declaring an array, we cannot change the size of the array. That means we can’t reduce the size nor increase the size of an array.

Elements of an array will be allocated contiguously in memory

When we create an array then each element of an array will be allocated in contiguous memory locations.

Contiguous memory locations means that just after the first element of an array, second element will be present in the memory. And just after the second element, third element will be present, and so on.

All the elements will be allocated in the memory locations back to back.

The first element of an array will have the lowest address and the last element will have the highest address.

Elements of an array are accessed by an index

Elements of an array are accessed by an index. The first element will have the index 0, second element will have the index 1, third will have the index 2 and so on. The last element will have index (n-1) where n is the number of elements in an array.

  • Is array a collection of similar objects?
  • Are arrays fixed in size?
  • Will elements of an array be allocated contiguously in memory?
  • Are elements of an array accessed by an index?

If you say YES for all the above questions then the definition of an array will be

An array is a fixed collection of similar objects stored contiguously those are accessed by an index

Array declaration

The declaration of arrays uses the following syntax:

elements_type name_of_array [ number_of elements]

Here are some examples of array’s declaration:

//declare an array of 100 elements of the type int
int myIntArray[100];
//declare an array of 5 elements of the type double
double myDoubleArray[5];

When you declare an array the allocation of the memory happens in the Stack Memory. The memory layout for array is contiguous. The identifier of the array (its “name”) is the start address of the array in memory. The memory layout of the array double myDoubleArray[5] can be demonstrated by the following picture

Every “cell” of the array has size of double. You have to remember that the numeration of the array starts with 0 and ends with number of elements – 1.
To use the declared array you have to initialize it.

Array initialization

There are several ways to initialize an array:

    1. By using one statement with the square brackets:
int anotherIntArray[3] = { 1, 2, 5, 7 };
    1. If you initialize an array in this way, you will be able to omit the size of array in the declaration:
int anotherIntArray[] = { 1, 2, 5, 7 };

The size of array will be set automatically according to the number of elements. This array will be of size 4.

Accessing array’s elements

Accessing elements of the array directly. It can be done by using indices of the array to access elements.

When you have to work with array, you need to access array’s elements. It can be done be using index of the element in brackets with array’s name. The index is the position of the element in array. In other words, index is the offset of the element relative to the start of the array. The numeration of the elements in array starts with 0, so the first element of an array has index 0. Here is the example of memory layout and indices of a array of 5 elements of type double:

This is an example of accessing elements of an array in a loop:

//display all values of an array in a loop
for (int i = 0; i < 4; ++i)
cout << "Element with index " << i <<" is "<< anotherIntArray[i] << endl;

This loop accesses element using index i. Take a look on the output:

Element with index 0 is 1
Element with index 1 is 2
Element with index 2 is 5
Element with index 3 is 7

You can’t use a negative number for an index. Also, you can’t use the index that is more than size of array – 1. If you try to do that then you will access a part of memory that is located near your array. It can produce absolutely fatal results for your program.

Passing Array to a Function

You can pass your array to a function. To pass an array to a function you just need to add it to the parameter list. This is a simple example of function that takes an array as argument:

void passArray(int arr[], int size)
{
	for(int i = 0; i != size; ++i)
		cout << "Array[" << i << "] = " << arr[i] << endl;
}

To call this function with parameters just pass your array as a simple parameter:

passArray(anotherIntArray, 4);

Return Array from a Function

There is a possibility to return an array from a function. But it is done by using pointers and is discussed at Return Pointer to Array from Function.

Multidimensional Array

C++ allows you to create multidimensional array. A multidimensional array is an array with elements that are arrays too. A simple example of multidimensional array is a 2 dimensional array that represents a matrix. In this array elements are 1 dimensional array. You have to use the following syntax for multidimensional array’s declaration:

elements_type name_of_array [ number_of elements1] [ number_of elements2]… 
[ number_of elementsN]

This is a declaration of N dimensional array. Basically you will use 2 or 3 dimensional arrays in your programs.

2D arrays

2D arrays represent the matrix. For any element in the array, the first index in the square brackets is the number of rows and the second index is the number of columns of that element. You can declare a 2D array in the following way:

//2d array of ints
int array2D[3][2];

This declaration means that you declare an array with 3 rows and 2 columns. You can imagine this array in the following way:

Even though it’s a 2D array, but the memory layout for this above array will be contiguous:

Here is an example of accessing 2 dimensional array:

//initialize 2D array:
for (int i = 0; i < 3; ++i)
	for (int j = 0; j < 2; ++j)
		array2D[i][j] = i + j;
//display 2d array
for (int i = 0; i < 3; ++i){
	for (int j = 0; j < 2; ++j)
		cout << array2D[i][j] << "  " ;
	cout << endl;

The output for displaying 2D array:

0  1
1  2
2  3

3D array

3D array is an array with 3 indices:

//3d array of ints
int array3D[3][5][4];

There are several ways to imagine what a 3D array is. We would like to recommend you to imagine a 3D array as a book of the tables with the same number of rows and columns on each page. In this case the first index is the number of page, the second index is the number of row on the page and the third one is the number of column on the page:

Initialization of 3D array:

	
for (int i = 0; i < 3; ++i)
	for (int j = 0; j < 5; ++j)
		for (int k = 0; k < 4; ++k)
			array3D[i][j][k] = i + j - k;

The memory layout for 3d arrays as for all arrays is contiguous.

You can print this array in the following way:

for (int i = 0; i < 3; ++i){
	cout << "Page #" << i << endl;
	for (int j = 0; j < 5; ++j)
	{
		for (int k = 0; k < 4; ++k)
			cout << array3D[i][j][k] << " ";
		cout << endl;
	}
}

The output for 3D array is:

Page #0
0  -1  -2  -3
1  0  -1  -2
2  1  0  -1
3  2  1  0
4  3  2  1
Page #1
1  0  -1  -2
2  1  0  -1
3  2  1  0
4  3  2  1
5  4  3  2
Page #2
2  1  0  -1
3  2  1  0
4  3  2  1
5  4  3  2
6  5  4  3

As you can see, a 3D array is an array of 2D arrays.

Translate »