In this article, we are going to learn various ways through which we can find array length in C++. The length of an array refers to the total number of elements present in the corresponding array. For example, take a look at the array below:
The size or length of the array here is equal to the total number of elements in it - which is 5.
There are a few methods through which we can determine the length of an array in C++ language. They are:
begin()
and end()
functionssizeof()
functionsize()
function in STLNow, let us discuss each method one by one with examples and in detail.
Traversing throughout the given array and simultaneously counting the total number of elements we traversed can give us the array’s length. But if we do not know the array’s length, we cannot use a for loop to traverse the array since a for loop needs a terminating number. This issue can be solved by using a simple for-each loop. Let’s take a look at the code below.
If we run this code, w ewill get the following output:
In this code, we traverse the array arr
using a for-each loop with i as the iterator. As the loop traverses, c is incremented. When the loop terminates, the c variable contains the number of times the loop was executed, giving the total length of the array.
We can also calculate the length of an array using the standard library’s begin()
and end()
functions. The two functions return iterators pointing to the corresponding array’s start and the end, respectively. Take a look at the given code:
The output of this code will be:
Here, we can see the difference between the return values of the two functions end()
and begin()
gives us the size or length of the given array arr
. In this case, the difference is 4, which is the length of arr
.
The sizeof()
operator in C++ returns the size of the passed variable or data in bytes, plus the total number of bytes required to store an array. So, if we divide the size of the array by the size acquired by each element of the same, we can get the total number of elements present in the array.
Let us take a look at how it works.
The output of this code will be:
As we can see, we get our desired length as output.
There is a size()
function defined in the standard library that returns the number of elements in the given container(array in our case). We can use this function to return the length in the following way:
On execution, the above code will return the following output:
We can also find the length of a given array using pointers. A pointer is a variable that stores the memory address of the object instead of storing the object itself. Let us see how we can use a pointer to get the length of an array.
The output of this code will be:
The expression *(arr+1)
gives us the address of the memory space just after the array’s last element. Hence, the difference between it and the array’s starting location or the base address (arr
) gives us the total number of elements in the given array.
In this tutorial, we discussed the various methods to find array length in C++. Even though all methods return the length in one way or the other, we prefer using for-each loop not only because of code readability but also for its cross-platform reliability.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.