C++ pass array by reference

arrays, c++, reference

Solution

They are different. The first one takes a reference to an array of 4 ints as its argument. The second one takes a pointer to the first element of array of an unknown number of ints as its argument.

int array1[4] = {0};
int array2[20] = {0};

void myFunction1( int (&arg)[4] );
void myFunction2( int arg[4] );

myFunction1( array1 ); // ok
myFunction1( array2 ); // error, size of argument array is not 4

myFunction2( array1 ); // ok
myFunction2( array2 ); // ok

Problem

Possible Duplicate: What is useful about a reference-to-array parameter? Are ``` void myFunction( int (&arg)[4] ); ``` and ``` void myfunction(int arg[4]); ``` different? How are they different? What do the first do and how can I call it?

Original source

Related problems