< C++ Tutorial

C++ arrays

← Previous Next →


Array is a homogenous collection of variables i.e. set of variables having same data type. 

  • Array might be belonging to any of the data types
  • Array size must be a constant value.
  • Contiguous (adjacent) memory locations are used to store array elements in memory.

Example:

  • int number[10];       // integer array
  • char name[10];      // character array   i.e. string

Types of C++ arrays:

  1. One dimensional array
  2. Multi dimensional array
    • Two dimensional array
    • Three dimensional array, four dimensional array etc…

One dimensional array in C++:

Syntax : data-type arr_name[array_size];

Example : int number[5];

Array element can be accesessed using index. 
0th element : number[0];
1st element : number[1];
.
.
4th element : number[4];

index go from 0 - (n-1) , where n is length of array. 
number[0] number[1] number[2] number[3] number[4]

Multi-dimensional array in C++:

Syntax : data-type arr_name[size1][size2][.]..[];
Example : Two dimensional array

int person[2][3];

person[0][0] person[0][1] person[0][2]
person[1][0] person[1][1] person[2][2]


Where 2 is number of rows and 3 is the number of elements in each row(i.e. number of columns).

Note: Similarly 3 dimensional and higher dimension arrays can be used.

← Previous Next →