Array in C++
Array in C++
In C++, an array is a data structure that is used to store multiple values of similar
data types in a contiguous memory location. array in C++ is a group of similar
types of elements that have contiguous memory location.
ANSHUL JAIN
We have now declared a variable that holds an array of four strings. To insert
values to it, we can use an array literal - place the values in a comma-separated
list, inside curly braces:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
You access an array element by referring to the index number inside square
brackets [].
cars[0] = "Opel";
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
ANSHUL JAIN
An array can have multiple dimensions.
The size of the array in bytes can be determined by the sizeof operator using
which we can also find the number of elements in the array.
int main()
int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
return 0;
#include <iostream>
int main()
int arr[] = { 1, 2, 3, 4, 5 };
ANSHUL JAIN
// Size of one element of an array
// Length of an array
return 0;
ANSHUL JAIN
1. Multi-dimensional array: It is an array that contains one or more arrays as
its elements. In this type of array, two indexes are there to describe each
element, the first index represents a row, and the second index represents
a column.
data_Type array_name[n][m];
Where,
n: Number of rows.
m: Number of columns.
#include <iostream>
int main()
// Declaring 2D array
int arr[4][4];
arr[i][j] = i + j;
ANSHUL JAIN
}
return 0;
ANSHUL JAIN