C++ Array Basic

In the previous topic we have learnt about the functions in the C++ programming language. In this tutorial we are looking for some basics of array.

C++ Array :

Suppose we need to store 100 roll numbers then we need to declare the 100 variables to keep track of the 100 students. Accessing variables is also difficult in this case. All the variables are also stored at random address in the memory.

Instead of declaring roll1,roll2,roll3…. we can directly declare roll[100] in which roll[1],roll[2]….are considered as individual variables.

What is Array ?

  1. Array is an data structure
  2. Array is collection of elements of same data type or we can also say that array is collection of variables of same type.
  3. Array stores the elements in the sequential manner
  4. Array elements are accessed randomly using the subscript or index variable
  5. Array elements consist of contiguous memory locations.
  6. Lowest address of an array corresponds to the first element.
  7. Highest address of an array corresponds to the last element.

Recommanded Tutorial : What is subscript variable ?

Declaring Arrays:

In order to declare an array in C++, we need to specify the type of the elements and the number of elements that we require

data-type arrayName [arraySize];

Suppose we need to declare the array of students then we can use –

int roll[5]; // We created array of 5 students

In the above case we have 5 roll numbers

Element Description
roll[0] It represents variable for 1st roll number
roll[1] It represents variable for 2nd roll number
roll[2] It represents variable for 3rd roll number
roll[3] It represents variable for 4th roll number
roll[4] It represents variable for 5th roll number

Consider the below representation –
cpp array

Initializing Arrays:

Consider that we need to initialize array then we can initialize the array using the 3 ways.

Way 1 : Specify Size and initialize in 2 steps

In this case we declare the array in one line and initialization will be done using the loop.

int roll[5];
    cin >> roll[i];

Way 2 : Specify Size and initialize in 1 step

In this case we declare and initialize array in single statement,

roll[5] = {11,22,33,44,55};

Here we have assigned following values to array element –

Element Description
roll[0] 11
roll[1] 22
roll[2] 33
roll[3] 44
roll[4] 55

Way 3 : Without Specifying Size

In this case we just do assignment and initialization but compiler will automatically calculate the size of the array at compile time.

roll[] = {11,22,33,44,55};

Above statement will specify the size of array equal to 5.

C++ Accessing Array :

#include <iostream>
using namespace std;
int main ()
   // roll is an array of 10 integers
   int roll[10]; 
   // initialize elements of array          
   for (int i = 0; i < 10; i++) {
      roll[i] = i + 1;
   cout << "Element \t \t Value" << endl;
   for (int i = 0; i < 10; i++) {
	   cout << i << "\t \t" << roll[i] << endl;
   return 0;

Output :

Element Value
0          1
1          2
2          3
3          4
4          5
5          6
6          7
7          8
8          9
9          10