C++ Arrays

An array is a special type of variable used to store multiple values of same data type at a time.

When we work with a large number of data values, we need any number of different variables. As the number of variables increases, the complexity of the program also increases. There may be situations where we need to work with a large number of similar data values. This work made easy with the concept called "Array" in C++.

What is an Array?

An array is a special type of variable used to store multiple values of same data type at a time. An array may aslo defines as a collection of similar data items stored in continuous memory locations with single name.


How to create an array in C++?

When we want to create an array, we must know the data type of values to be stored and also the number of values to be stored. We use the following syntax to create an array.

Syntax
data_type arrayName[size];
or
data_type arrayName[size] = {value1, value2, value3,...};
or
data_type arrayName[] = {value1, value2, value3,...};

In the above syntax, the data_type specifies the type of values we store in that array, and size specifies the maximum number of values that the array stores. We may use any syntax to create an array.

  • When an array is created without any specific size, it must be initialized. However, it is optional to initialize if the size has been specified.
  • When an array is created without a specific size, then the size of that array is decided by the compiler based on the number of data values initialized.
Example
string fruits[5];
string colors[5] = {"Red", "Green", "Blue", "Yellow", "Orange"};
string mobiles[] = {"iPhone", "Samsung", "Vivo", "Nokia", "Oppo", "One Plus"};

In the above example, the "mobiles" array was created without any size. But initialized with 6 elements, so the size of it is decided by the compiler as 6.


How to use individual elements of an array?

When an array has been created, it allocated with the memory as follows.

arrays in C++

The individual elements of an array are identified using the combination of 'arrayName' and 'index number'. We use the syntax like arrayName[indexNumber] to represent an individual element of an array. Using the same we may access or change the element value.

The individual elemetns of the colors array are accessed as shown in the below figure.

Accessing-Array-Elements-with-index
Example code
#include <iostream>

using namespace std;

int main()
{
    string colors[5] = {"Red", "Green", "Blue", "Yellow", "Orange"};

    cout << "First element is " << colors[0] << endl;   // Accessing the first element

    colors[3] = "Pink";     // Value at index 3 (Yellow) changed to Pink

    cout << "Value at index 3 is " << colors[3] << endl;

    return 0;
}
Output
program to illustrate accessing array elements

Looping through an array

We use the for statement to loop through an array. check the following code to loop through the array colors.

Example code
#include <iostream>

using namespace std;

int main()
{
    string colors[5] = {"Red", "Green", "Blue", "Yellow", "Orange"};

    for(int i = 0; i < 5; i++) {
        cout << "colors[" << i << "] = " << colors[i] << endl;
    }

    return 0;
}
Output
looping-through-an-array
  • In C++, there is no bondary checking. We may access the memory locations beyond the index numbers which lead to unexcepted results.
  • When an array is created with specific size and initial values then number of values must equal or less than the size but not more than the size.
  • Address of the first element is called base address and it is store in a pointer with the array name automatically.
  • The array elements stored in contiguous memory locations.