There are several ways to compute the length of an array in C++.
sizeof()
One way to find the length of an array is to divide the size of the array by the size of each element (in bytes).
The built-in sizeof()
operator is used for this purpose (shown in the code snippet below):
#include <iostream>using namespace std;int main() {int arr[] = {10,20,30,40,50,60};int arrSize = sizeof(arr)/sizeof(arr[0]);cout << "The size of the array is: " << arrSize;return 0;}
Since we have a pointer at the start of the array, the length of the array can be calculated if we manage to find out the address where the array ends. This is done as follows:
int arrSize = *(&arr + 1) - arr;
Let’s break down the above line of code:
(&arr + 1)
points to the memory address right after the end of the array.*(&arr + 1)
simply casts the above address to an int *
.Here’s how it’s implemented:
#include <iostream>using namespace std;int main() {int arr[] = {10,20,30,40,50,60};int arrSize = *(&arr + 1) - arr;cout << "The length of the array is: " << arrSize;return 0;}