We can pass std::array to function by either pass by value or reference or address. std::array requires element type and array length as part of type information, we need to provide element type and array length while we define parameter of std::array in the function. For example,

#include <iostream>
#include <array>
 
void printArray(const std::array<int, 5> &arr)
{
    for (auto ele : arr)
    {
        std::cout << ele << " ";
    }
 
    std::cout << '\n';
}
 
int main(int argc, char const *argv[])
{
    std::array items{1, 2, 3, 4, 5};
 
    printArray(items);
    return 0;
}

Note

This is because CTAD does not work (in current versions at least till C++23) with deducting the type for function parameter.

As this function only works with array of type int and length 5, we can create a function template to make it work for other types of array.

#include <iostream>
#include <array>
 
template <typename T, std::size_t N>
void printArray(const std::array<T, N> &arr)
{
    for (auto ele : arr)
    {
        std::cout << ele << " ";
    }
 
    std::cout << '\n';
}
 
int main(int argc, char const *argv[])
{
    std::array items{1, 2, 3, 4, 5};
 
    printArray(items);
    return 0;
}

There are two types for template: normal type template parameter and non-type template parameter of type std::size_t (because std::array length is of std::size_t type).

Please note that we can’t provide any other type for N. If we do, the compiler would raise error as it can’t find a matching template. If we do not know which type to use for N, in C++ 20 and onwards, we can use auto instead.

For example:

 
template <typename T, auto N>
void printArray(const std::array<T, N> &arr)
{
    for (auto ele : arr)
    {
        std::cout << ele << " ";
    }
 
    std::cout << '\n';
}

References

  1. https://www.learncpp.com/cpp-tutorial/passing-and-returning-stdarray/