C++ Arrays

In this tutorial, we will learn about arrays and how to declare an array in C++ with the help of examples.


A collection of similar data objects in C++ is known as an array. These data items are stored in a contiguous memory location which allows to access any element of an array with O(1) time complexity or in simple words, you can jump to any element of an array with the proper index.

You can declare an array of integers, float, double, and many more. Pointers also can be stored in an array. You need to remember that in C++, like any other programming language, the first index always begins at 0 and not 1.


Advantages of using an Array

There are multiple advantages of using an array in C++ and some of them are listed below:

  • Random access to any element of an array.
  • You can declare multiple variables with one array and hence it is code efficient.
  • Can access all elements using a single loop.
  • Sorting and searching become easy and can be done in fewer lines of code.
  • The name of the array is a pointer to the first location of the array.

Disadvantages of using an Array

The following are the disadvantages of using an array

  • The length of an array is fixed and cannot be changed.
  • Insertion and deletion are costly operations on an array.

C++ Array Types

Arrays in C++ can be divided into the following two categories:

  1. Single Dimensional Array
  2. Multidimensional Array

C++ Single Dimentional Array Declaration

Generally when your program has hundreds of variables or you need to declare many variables to store a value, instead of declaring that many variables, you declare an array and then use the index to access all of its elements one by one. There are many ways to declare an array out of which some are given below:

Array Declaration by mentioning the size of the array

// Declaring an array with a specific size
int arr[5];
// Works in the newer versions of C++ where n needs to be initialized
int n =10;
int arr[n];

Array Declaration by initializing the elements

// Specifying the elements of the array
int arr[] = {1,2,3,4}
// Specifying the size and elements of the array
int arr[4] = {1,2,3,4}
// Specifying the size and giving elements less than that
int arr[5] = {1.2.3.4}
// The array actually becomes {1,2,3,4,0} as the compiler fills the blank values with the zero.

C++ Single Dimentional Array Example

#include <iostream>
using namespace std;
int main()
{
    int arr[5];
    arr[0] = 1;
    arr[2] = -1;
    // this is the same as arr[1] = 2
    arr[5/3] = 2;
    arr[3] = arr[0];
    cout << arr[0] << " " << arr[1] << " " << arr[2] << " "<< arr[3];
    return 0;
}

Output

1 2 -1 1

Let’s take another simple example of array where we will create, initialized and traverse the array.

#include <iostream>
using namespace std;
int main()
{
    int arr[5] = {5, 10, -10, 20, 30}; //creating and initializing array
    //traversing array
    for (int i = 0; i < 5; i++)
    {
        cout << arr[i] << "\n";
    }
}

Output

5
10
-10
20
30

C++ has no array out of bounds checking

You need to manually code to check if you are indexing any element outside the bounds of an array as C++ does not do that for you. The following code shows that case:

#include <iostream>
using namespace std;
int main()
{
    int arr[2];
    cout << arr[30] << " ";
    cout << arr[-20] << " ";
    return 0;
}

Output

0 32

This is because garbage value is stored in that location but however if you declare an array of length 2 and try to assign more than 2 values into it, then it will give an error.

Please get connected & share!

Advertisement