Updated June 30, 2023
Introduction to C++ Length of Array
Arrays are used to make the work of assigning numerous values simpler. If we have to assign a huge amount of values, creating variables for every value is not an intelligent task to do. For this, we can assign an array that can hold all of the values, and then the different values can be accessed in the array using indexing. In this article, we are discussing arrays and how to find the length of an array. There are different methods to determine the length of an array that can be used according to the convenience of the coder. All of the methods are explained with an example for better understanding. In this article, we will discuss the C++ Length of an Array in detail.
Syntax :
The basic syntax used to find the length of an array in C++.
int array[] = {1,2,3,4,5,6};
int arraySize = sizeof(array)/sizeof(array[0]);
Examples of C++ Length of Array
The various methods which can be used for finding the length of an array are discussed below with examples.
Example #1 – With the Help of Size of Operator
The first and easiest method for finding out the length of an array is by using sizeof() operator. In the example below, we have created an array named “ EDUcba” in which there are 10 integers stored. Then, we created a variable “stretch” which calculates the length of the array “EDUcba.” For calculating the length of the array “ EDUcba”, we have calculated the array size and then divided it by the size of an element of the array. This calculation results in the length of the array “ EDUcba.”
Code:
#include <iostream>
using namespace std;
int main() {
int EDUcba[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
int stretch = sizeof(EDUcba)/sizeof(EDUcba[0]);
cout << "Array is consists of: " << stretch << " numbers" << endl;
cout << "Hence, Length of Array is: " << stretch;
return 0;
}
Output:
Example #2 – With the Help of Pointers
For finding the length of an array, we can also use pointers. In the example below, once again, we have created an array of ten integers and have named the array “ EDUcba.” We also have created a variable named “stretch” which calculates the length of the array. While calculating stretch, we have used *(&EDUcba + 1), which contains the address after the ten elements of the array “ EDUcba.” Now, the value contained in EDUcba is the starting address of the array. When we subtract these two, we get the length of the array.
Code:
#include <iostream>
using namespace std;
int main() {
int EDUcba[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
int stretch = *(&EDUcba + 1) - EDUcba;
cout << "Array is consists of: " << stretch << " numbers" << endl;
cout << "Hence, Length of Array is: " << stretch;
return 0;
}
Output:
Example #3 – Self-Defined Sizeof
For finding out the length of an array, we can also define our own function of the size and we can then use it to find the length of the array. In this example, we have defined “measure_length” which works in the same way as the size_of operator. The “stretch” variable then calculates the length of the array.
In the example below,
Code:
// finding size of array using measure_length
//function defined in place of sizeof
#include <bits/stdc++.h>
using namespace std;
// sizeof defined by us
# define measure_length(type) ((char *)(&type+1)-(char*)(&type))
int main()
{
int eduCBA[] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
int stretch = measure_length(eduCBA)/measure_length(eduCBA[0]);
cout << "The total members in the array are: " << stretch << endl;
cout << "Hence, Length of Array is: " << stretch;
return 0;
}
Output:
Example #4 – Template argument Deduction Method
When an array is passed to a function as an argument in C++, the array then decays into a pointer. In the example below, we have used the same concept to determine the length of the array.
Code:
#include <iostream>
template <class X, std::size_t Y>
constexpr std::size_t length(const X (&array)[Y]) noexcept
{
return Y;
}
int main()
{
int eduCBA[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
std::cout << " The total members in the array are: " << length(eduCBA);
return 0;
}
Output:
Example #5 – Using std::distance
We can use std::distance for determining the length of an array. It provides iterators between the array’s starting element and the last element. These iterators then provide the total number of hops between them.
Code:
#include <iostream>
#include <iterator>
int main()
{
int eduCBA[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int X = std::distance(std::begin(eduCBA), std::end(eduCBA));
std::cout << "The total members in the array are: " << X << " ..............";
std::cout << " Hence, Length of Array is: " << X;
return 0;
}
Output:
Example #6 – Usage of.Pointer Arithmetic
We can also use the pointer arithmetic to find the length of an array. In this, an integer “X” calculates the length of the array “eduCBA” by using the iterators, which captures the address of the first and last elements and then subtracts the two values. It will result in the length of the array.
Code:
#include <iostream>
#include <iterator>
int main()
{
int eduCBA[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int X = std::end(eduCBA) - std::begin(eduCBA);
std::cout << "The total members in the array are: " << X << " ..............";
std::cout << " Hence, Length of Array is: " << X;
return 0;
}
Output:
Example #7 – Using std::size()
In C++17, there is a template function std::size. It is in the <iterator> header, and it provides the total number of elements in an array. In this example, we have used the same concept. Here, the std::size calculates the total number of elements in the array, which gives us the length of the array.
Code:
#include <iostream>
#include <experimental/iterator>
int main()
{
int eduCBA[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
std::cout << "The total members in the array are: " << std::size(eduCBA);
return 0;
}
Output:
Conclusion
On the basis of the above article, we can understand the concept of arrays and the various methods to find their length. This article discusses the different methods of finding the length of an array and also explains how the methods can be used through the different examples written above.
Recommended Articles
This is a guide to C++ Length of Array. Here we discuss the introduction and Finding the length of an array in C++ with examples respectively. You may also have a look at the following articles to learn more –