When an array is passed by value as an argument of a function it implicitly is converted to pointer to its first element. Also parameters that declare arrays are adjusted to pointers.
So for example these function declarations
void printarray( int array[100] );
void printarray( int array[10] );
void printarray( int array[] );
declares the same one function and equivalent to
void printarray( int *array );
So you need to pass also the size of the array to the function as for example
void printarray( const int array[]. size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
std::cout << a[i] << std::endl;
}
}
You could write a template function specially for arrays passed by reference as for example
template <size_t N>
void printarray( const int ( &array )[N] )
{
for ( int x : array)
{
std::cout << x << std::endl;
}
}
or
template <typename T, size_t N>
void printarray( const T ( &array )[N] )
{
for ( auto x : array)
{
std::cout << x << std::endl;
}
}
However compared with the previous function it has a drawback because arrays of different sizes are different types and the compiler will generate as many functions from the template as many arrays of different types you are going to use with the function.
And you could use standard algorithms as for example std::copy
or std::for_each
to output an array.
For example
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::copy( std::begin( array ), std::end( array ),
std::ostream_iterator<int>( std::cout, "\n" ) );
return 0;
}
Another approach is to use standard class std::array
that has appropriate member functions begin
and end
that are used by the range based for statement. For example
#include <iostream>
#include <array>
const size_t N = 10;
void printarray( const std::array<int, N> &array )
{
for ( int x : array ) std::cout << x << std::endl;
}
int main()
{
std::array<int, N> array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printarray( array );
return 0;
}
But in this case you need also to write a template function if you are going to output objects of class std::array
with different numbers or types of elements.
For example
#include <iostream>
#include <array>
template <typename T, size_t N>
void printarray( const std::array<T, N> &array )
{
for ( auto x : array ) std::cout << x << std::endl;
}
int main()
{
std::array<int, 10> array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printarray( array1 );
std::array<char, 10> array2 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
printarray( array2 );
return 0;
}