Introduction to Arrays in C++ language
An array is defined as a linear collection of similar data items, which are stored in continuous memory locations. In other words we can say that an array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. The array of character is known as "string" whereas the array of integers and floats are known as simply array.
It should be important to note that all the elements of the arrays are of same type i.e we can't have an array of 20 elements, of which 10 are of integer type and 10 are of float type. All the elements of the array are accessed using the same variable name followed by different subscript index which is further written in square brackets. The subscript number of first element of the array in C++ is always 0 and the last index of the array element is 1 less than the total number of elements.
Let's have a look on array and how elements are stored in it.
In the given example here, single variable is used to store the age of the person.
#include<iostream.h>
int main()
{
int age=20;
cout<<age<<endl;
return 0;
}
The single variable stored in memory like this:
Array is the way which store the age of multiple persons under the single variable name as given below:
#include<iostream.h>
int main()
{
int age[6];
age[0] =20;
age[1]=16;
age[2]=15;
age[3]=17;
age[4]=18;
age[5]=21;
return 0;
}
The array elements are stored in memory like this:
C++ further support three types of arrays:
1. 1-Dimensional Arrays
2. 2-Dimensional Arrays
3. Multidimensional Arrays
The arrays created in C++ can be a static reference which means the memory once allocated to an array cannot be modified. In dynamic arrays references, the memory allocation for the arrays can be changed during the program run time. The dynamic array references are created using pointers and manipulated using new and delete operator.
Thanks
Mukesh Rajput
Thanks Mukesh for sharing some valuable content
ReplyDelete