Arrays in C++
In this article, we will be learning how to work with arrays in C++. An array is a set of values of the same data type stored in contiguous memory locations.
Let’s suppose we need to store 10 values of int type, instead of declaring 10 different variables, we can declare an int array of size 10.
C++ Array Declaration and Initialization
Syntax:
datatype arrayname[size];
For example, we can declare an array named arr that can store 10 elements of int data type as:
int arr[10];
We can initialize an array while declaring it.
Array declaration and initialization can be done in following ways:
1. Specifying size
int arr[5] = {5,2,6,9,12};
arr[0] arr[1] arr[2] arr[3] arr[4]
5 | 2 | 6 | 9 | 12 |
0 1 2 3 4
2. Without specifying size
int arr[] = {5,2,6,9,12};
This will also result in the same array as shown above.
3. Initializing fewer elements than specified size
In this case, remaining elements store the default value (for fundamental data types, it is 0).
int arr[5] = {5,2,6};
arr[0] arr[1] arr[2] arr[3] arr[4]
5 | 2 | 6 | 0 | 0 |
0 1 2 3 4
4. No value in initializer
int arr[5] = { };
This will result in an array arr of size 5 whose each element initially contains zero.
arr[0] arr[1] arr[2] arr[3] arr[4]
0 | 0 | 0 | 0 | 0 |
0 1 2 3 4
Data Type
An array in C++ can contain elements of any data type including int, float, double, char, etc. except void.
Accessing Array Elements in C++
We can access an array element using its index. Index of an array starts from 0 and ends at (size-1).
Syntax:
arrayname [index];
Example of accessing array elements in C++
//program to change value of an array element #include <iostream> using namespace std; int main() { int arr[5] = {3,7,10,9,14}; //changing value of 3rd element arr[2] = 11; //Displaying third value cout<<arr[2]; return 0; }
Output
Example to store user inputs in an array and display array elements
#include <iostream> using namespace std; int main() { int arr[5]; //Taking inputs from user and storing in arr cout<<"Enter 5 values"<<endl; for(int i=0; i<5; i++) { cin>>arr[i]; } //Displaying array elements for(int i=0; i<5; i++) { cout<<arr[i]<<" "; } return 0; }
Output
1
3
5
6
2
1 3 5 6 2
Multidimensional Arrays in C++
Multidimensional array can also be referred to as an array of arrays.
We can think of a 2-Dimensional array as a table of elements of the same data type. Let us declare a 2-D array for better understanding.
int x[3][4];
This array contains 12 elements (as 3 * 4 = 12) of int data type. It can be thought of as a table having 3 rows and 4 columns, where each row is an array of 4 elements in itself.
Example to initialize a 2-D array and access its element
#include <iostream> using namespace std; int main() { //Initializing int arr[2][4] = {{2,9,4,6}, {1,10,8,5}}; //Displaying 3rd element of 1st row cout<<arr[0][2]; return 0;
Output
Passing Array to a Function in C++
When we pass an array to a function in C++, the function treats it as a pointer. This means that the actual array is accessed from inside the function.
Example of passing array to a function
#include <iostream> using namespace std; void display(int a[], int n) { cout<<"Elements of array are: "<<endl; for(int i=0; i<n; i++) { cout<<a[i]<<" "; } } int main() { int arr[5] = {1, 2, 4, 8, 16}; unsigned int n = sizeof(arr)/sizeof(arr[0]); //to get number of elements in array display(arr, n); return 0; }
Output
1 2 4 8 16
In the above example, we have declared the formal parameter as an unsized array. This can also be declared as a pointer or a sized array.
For example:
void display(int *a, int n) { //statements } or void display(int a[5]) { //statements }
In each of these three ways, the result obtained will be the same, as each indicates an integer pointer to the compiler.
Pointer to an Array in C++
An array and its elements can be accessed using pointers.
Example of pointer to an array in C++
#include <iostream> using namespace std; int main() { int arr[3] = {2, 4, 8}; int *p; p = arr; //pointer p points to 0th element cout<<*p<<endl; //To display all elements for(int i=0; i<3; i++) { cout<<*(p+i)<<" "; } return 0; }
Output
2 4 8
In this example, pointer p points to the 0th element of the array. However, we can also declare a pointer that points to an entire array.
For example
int (*ptr)[3];
This pointer ptr points to an array of 3 integers.
Example to illustrate implementation of pointer pointing to an entire array
#include <iostream> using namespace std; int main() { int arr[3] = {2, 4, 8}; int (*p)[3]; p = &arr; //pointer p points to entire array for(int i=0; i<3; i++) { cout<<(*p)[i]<<endl; } return 0; }
Output
4
8
Advantages of Arrays in C++
Advantages of C++ arrays are:
- Requires less lines of code, because it enables us to create an array of numerous elements.
- Array indexes allow for random access to elements. Hence, elements are easily accessible.
- Traversing an array is simple.
- Sorting becomes simple.
- We can implement other data structures using arrays.
- 2-D arrays represent matrices.
Disadvantages of arrays in C++
- While declaring an array, its size is fixed. We cannot modify the size of an array.
- There is wastage of memory if we declare size more than required.
- Insertion and deletion become costly as elements are needed to be shifted accordingly.
Summary
We learnt about arrays in this article. An array is a set of values of the same data type stored in contiguous memory locations.
Elements of an array can be accessed using index. We learnt how to declare an array, and initialize and access its elements in C++ with suitable examples. Then, we also discussed multidimensional arrays, followed by advantages and disadvantages of arrays.