An array is a data structure used for storing a collection of values that all have the same data type. An array can be imagined as a series of data storage locations. Each storage location is called an element of the array.
In C++, array index starts from 0. We can store only fixed set of elements in C++ array.
There are 2 types of arrays in C++ programming:
All the below sections are about single dimensional array.
An array can be declared as a normal variable declaration, but in addition append a set of square brackets following the array’s name. The brackets contain the number of elements in the array. The default values for these elements are the same as for variables. The elements in global arrays are initialized to their default values and elements in local arrays remain uninitialized.
int myArray[3]; // integer array with 3 elements
To assign values to the elements you can reference them one at a time by placing the element’s index inside the square brackets, starting with zero.
myArray[0] = 1; myArray[1] = 2; myArray[2] = 3;
You can initialize a simple array of built-in types, such as integers and characters by assigning values at the same time as the array is declared by enclosing them in curly brackets. The specified array length may optionally be left out to let the array size be decided by the number of values assigned.
int myArray[3] = { 1, 2, 3 }; int myArray[] = { 1, 2, 3 };
Once the array elements are initialized they can be accessed by referencing the index of the element you want.
std::cout << myArray[0]; // 1
The arrays in the previous sections are made up of static (non-dynamic) memory, their size must be determined before execution. Therefore, the size needs to be a constant value. To create an array with a size that is not known until run-time you need to use dynamic memory, which is allocated with the new keyword and must be assigned to a pointer or reference.
int* p = new int[3]; // dynamically allocated array
An array in C++ behaves as a constant pointer to the first element in the array. The referencing of array elements can therefore be made just as well with pointer arithmetic. By incrementing the pointer by one you move to the next element in the array, because changes to a pointer’s address are implicitly multiplied by the size of the pointer’s data type.
*(p+1) = 10; // p[1] = 10;
We can count the number of elements in an array using the built-in C++ function sizeof()
.
int post[] = { 10, 20, 30, 40, 50 }; const int size = sizeof(post) / sizeof(post[0]); //(or) int length = sizeof(post) / sizeof(int); // 5
This method cannot be used for dynamically allocated arrays. The only way to determine the size of such an array is through the variable used in its allocation.
int size = 3; int* p = new int[size]; // dynamically allocated array
When you are done using a dynamic array you must remember to delete it. This is done using the delete keyword with an appended set of square brackets.
delete[] p; // release allocated array
Let's see a simple example of C++ array, where we are going to create, initialize and traverse array.
#include <iostream> using namespace std; int main() { int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array //traversing array for (int i = 0; i < 5; i++) { cout<<arr[i]<<"\n"; } }
C++ Array Example: Traversal using foreach loop
We can also traverse the array elements using foreach loop. It returns array element one by one.
#include <iostream> using namespace std; int main() { int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array //traversing array for (int i: arr) { cout<<i<<"\n"; } }