# Find Array Length in C++

Filed Under: C++ ## Introduction

In this article, we are going to learn about the various ways following which we can find array length in C++.

Basically, when we say the length of an array actually we refer to the total number of elements present in the corresponding array. For example, for an array as given below:

```int array1[] = { 0, 1, 2, 3, 4 }
```

The size of the array or length of the array here is equal to the total number of elements in it. Which, in this case, is ‘5‘.

## Ways to find Length of an Array in C++

Now let us take a look at the different ways following which we can find the length of an array in C++, they are as follow:

1. Counting element-by-element,
2. `begin()` and `end()`,
3. `sizeof()` function,
4. `size() `function in STL,
5. Pointers.

Now, let us discuss each method one-by-one with examples and in detail.

### 1. Counting element-by-element

Traversing throughout the given array and simultaneously counting the total number of elements we traversed can give us the length of the array right?

But for traversing we cannot use a for loop directly if we do not know the array length beforehand. This issue can be solved by using a simple for-each loop. Look at the code below carefully.

```#include<iostream>
#include<array>
using namespace std;
int main()
{
int c;
int arr[]={1,2,3,4,5,6,7,8,9,0};
cout<<"The array is: ";
for(auto i: arr)
{
cout<<i<<" ";
c++;
}
cout<<"\nThe length of the given Array is: "<<c;

return 0;
}
```

Output:

```The array is: 1 2 3 4 5 6 7 8 9 0
The length of the given Array is: 10
```

Here, as we said, we traverse through the entire array arr using a for-each loop with iterator i. Simultaneously the counter, c is incremented. At last, when the traversing is over, c holds the length of the given array.

### 2. Using begin() and end()

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 start and the end of the corresponding array respectively. Take a close look at the given code,

```#include<iostream>
#include<array>
using namespace std;
int main()
{
//Given Array
int arr[] = { 11, 22, 33, 44 };

cout<<"The Length of the Array is : "<<end(arr)-begin(arr); //length

return 0;
}
```

Output:

```The Length of the Array is : 4
```

Hence, here as we can see, the difference between the return values of the two functions `end()` and `begin()` give us the size or length of the given array, `arr`. That is, in our case, 4.

### 3. Using sizeof() function to Find Array Length in C++

The `sizeof()` operator in C++ returns the size of the passed variable or data in bytes. Similarly, it returns the total number of bytes required to store an array too. Hence, if we simply 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 how that works

```#include<iostream>
#include<array>
using namespace std;
int main()
{  //Given array
int  arr[] = {10 ,20 ,30};

int al = sizeof(arr)/sizeof(arr); //length calculation
cout << "The length of the array is: " <<al;

return 0;
}
```

Output:

```The length of the array is: 3
```

As we can see, we get our desired output.

### 4. Using the size() function in STL

We have the `size()` function defined in the standard library which returns the number of elements in the given container(array in our case).

```#include<iostream>
#include<array>
using namespace std;
int main()
{  //Given array
array<int,5> arr{ 1, 2, 3, 4, 5 };
//Using the size() function from STL
cout<<"\nThe length of the given Array is: "<<arr.size();
return 0;
}
```

Output:

```The length of the given Array is: 5
```

As desired, we get the output as shown above.

### 5. Using Pointers to Find Array Length in C++

We can also find the length of a given array using pointers. Let us see how

```#include<iostream>
#include<array>
using namespace std;
int main()
{  //Given array
int  arr = {5,4,3,2,1,0};

int len = *(&arr + 1) - arr;
//*(&arr + 1) is the address of the next memory location
// just after the last element of the array

cout << "The length of the array is: " << len;

return 0;
}
```

Output:

```The length of the array is: 6
```

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 arrays starting location or the base address(arr) gives us the total number of elements present in the given array.

## Conclusion

So, in this tutorial, we discussed the various methods to find array length in C++. Even though each one of the above examples is easy to go with, we prefer the one using the for-each loop. Not only because of code readability but also for its cross-platform reliability.

For any further questions feel free to use the comments below.

## References

close
Generic selectors
Exact matches only
Search in title
Search in content