Home   |   About   |   Terms   |   Contact    
A platform for writers

Array in C++


☯ All C++ Articles     ☯ All Articles

Popular Google Pages:

◕ Send your story to RiyaButu.com and get ₹ 500/- Details..

◕ Bengali Story writing competition. Details..

◕ This article is regarding Array in C++.
Last updated on: .

◕ What is an array in C++?

An array is a data form in C++. This can hold several values of the same type.

In a class room there are 50 students.
We are making two arrays.

1. int student[50];
Here the array student is holding 50 different values of int type that represent the roll number of every student in a class room.

2. float weight[20];
Here the array weight is holding 20 different values of the float type that represents the weight of 20 different students in a class room.

◕ What is array element?

Arrays are made with the elements.
This means, each of the above mentioned values of the arrays are stored in separate elements.
The computer stores all the elements of an array consecutively in its memory.

float weight[20];
This array contains 20 elements. The elements are:
weight[0], weight[1], weight[2]... weight[18], weight[19]
The serial number of the element always starts from 0, not from 1.

◕ How to create an array?

Let have the examples first.
Here we are declaring two arrays.

■ 1st example:
int student[50];
The name of the array is student.
It has 50 elements.
Each element contains the age of a student.

■ 2nd example:
float weight[20];
The name of the array is weight.
It has 20 elements.
Each element contains the weight of a student.

Hence to create an array, we should declare a statement.
This array declaration consist three things.
1. The type of value to be stored in each element i.e. typeName.
2. The name of the array i.e. arrayName.
3. The number of the elements in the array i.e. arraySize.

The general declaration of an array is
typeName arrayName[arraySize];

Please note:
1. The arraySize must be an integer i.e. a constant value.
2. The arraySize can also be an expression like 2+sizeof(int).
But the result of the expression must be an integer.

Related articles:
How to display a value in Hexadecimal form in C++?
How C++ identify the base of a number?
Symbolic Constant in C++

Top of the page

Amazon & Flipkart Special Products


Top of the page