C ++ pass array by reference

Possible duplicate:
What is useful regarding the reference-to-array parameter?

are

void myFunction( int (&arg)[4] ); 

and

 void myfunction(int arg[4]); 

different? How different are they? What to do first and what can I call it?

+4
source share
1 answer

They are different. The first takes a reference to an array of 4 ints as an argument. The second takes a pointer to the first element of an array of an unknown number int as an 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 
+7
source

Source: https://habr.com/ru/post/1440330/


All Articles