Find the Length of an Array in C++

This tutorial will discuss multiple ways to find the length of an array in C++.

Table Of Contents

Suppose we have an array like this:

int arr[] = {78, 89, 10, 61, 55, 44};

Now we want to find the number of elements in this array. To do that, there are multiple ways. Let’s discuss them one by one.

Using the sizeof operator

In C++ to get the length of an array, we can use the sizeof operator. When we apply the sizeof operator on the array i.e. sizeof(arr), and it returns the total number of bytes used for the array arr. In this case, our array contains 6 elements, so sizeof array will return 24 bytes because the size of an integer is 4 bytes. If we divide the total size by the size of an integer, it will give us the total number of elements in the array. The expression to get the length of an array is like this:

// Get the length of Array in C++
int len = sizeof(arr) / sizeof(arr[0]);

The len will give us the total number of elements in the array.

Let’s see the complete example,

#include <iostream>

int main()
{
    int arr[] = {78, 89, 10, 61, 55, 44};

    // Get the length of Array in C++
    int len = sizeof(arr) / sizeof(arr[0]);

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

    return 0;
}

Output

The length of the array is: 6

Creating a generic template-based function

In this solution, we will create a generic template-based function that accepts an array as a reference and returns the size. When passing the array into the function as a reference, we need to provide 2 template parameters i.e. the data type of the array and the size of the array.

We will pass the array only, and the template function will deduce its size automatically. Inside the function, we will simply return the size. Now we can use this function to get the size of an array like this:

template <typename T, size_t N>
size_t getLength(T (&)[N])
{
    return N;
}

int arr[] = {78, 89, 10, 61, 55, 44};

// Get the length of Array in C++
size_t len = getLength(arr);

The len will give us the total number of elements in the array.

This function is more useful than the previously declared expression using sizeof because it’s an easy and more elegant way to get the size of an array or the number of elements in an array.

Let’s see the complete example,

#include <iostream>

// Template function to find the
// Length of an Array
template <typename T, size_t N>
size_t getLength(T (&)[N])
{
    return N;
}

int main()
{
    int arr[] = {78, 89, 10, 61, 55, 44};

    // Get the length of Array in C++
    size_t len = getLength(arr);

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

    return 0;
}

Output

The length of the array is: 6

Summary

We learned about two ways to get the length of an array in C++.

Summary

Today, we learned about multiple ways to find the length of an array in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top