Arrays in C++ – Part 1

Written by


Example: Now let us assume a situation, where you are the representative of your class and you are asked to store the details of all the students via a C++ program. Suppose there are 100 students in your class, to store the details of all of the students you need to declare 100 variables for each field. 100 for students’ name, 100 for Address, 100 for Serial Number and so on.

Here, is where the variables will help you out, instead of declaring separate variables for each entry you can easily create an array that will contain the information of all the students.

What is an Array?

Arrays are a special kind of data structures used in C++. They are user-defined data types, that is these are defined as per the need of the programmer. 

The type and size of an array can be defined by the user as per the need. Thus, there is no fixed size of an array and it can be of any built-in data types that we have discussed earlier.

So, in simple words, we can say that an array is a homogeneous collection of data.

Declaring an Array in C++:

Since we know the importance of an array, let us see how can we declare one in C++:

type array_name [array_size];

Here type is the data type of the array, array_name  is the name of the array and the rules are the same as that of naming a variable. array_size is the size of the array, that is how many elements an array will contain.


int arr[10];

The above example declares an integer array that can contain 10 integers.

Array Index:

The individual elements of this array can be accessed via array index. An array index starts from 0 and ends with array_size – 1. In the example, the indexing is from 0 to 9.


Array Part 2

Multidimensional Array

Arrays in C++ – Part 1