Here's a piece of my code that I divided. I basically need to create 3 functions: 1)1st to fill my array with random numbers 2)2nd to print the array on the screen 3)3rd isnt included in this piece.
My problem is that I get the C2664 error:cannot convert parameter 1 from int[6][6] to int(*)[]. And I cant figure what's wrong with my code. Also I would like to check if how I wrote my pointers to fill and print the array is correct.
Make life easier: define a type alias for the array int[6][6]
Pass the array by reference (to const).
To access its elements, use the simpler range based for loop.
Isn't passing an array to a function automatically pass it by reference? So you don't need the & on the parameters because you can't pass array by value, you just have to tell the parameter that i will receive an array.
> Isn't passing an array to a function automatically pass it by reference?
The only way to pass an array to a function is by reference. The reference syntax & has to be used.
> So you don't need the & on the parameters ...
> you just have to tell the parameter that i will receive an array.
If you omit the &, you are passing a pointer by value. The the actual argument is an array, an implicit array-to-pointer conversion is performed, and the resultant pointer is passed by value.
void foo( int (&array)[10] ) {} // pass an array of type int[10] by reference
void bar( int array[] ) {} // pass a pointer to int by value
void baz( int array[1000] ) {} // same as above; pass a pointer to int by value
void foobar( int array[99999] ) {} // same as above; pass a pointer to int by value
void foobaz( int* array ) {} // same as above; pass a pointer to int by value
int main()
{
int a[10] = {0} ;
int b[20] = {0} ;
foo(a) ; // fine; array of 10 int or int[10]
// foo(b) ; // *** error: type mismatch (not array of 10 int )
bar(a) ; // fine; array decays to a pointer, the pointer is passed by value
bar(b) ; // fine; array decays to a pointer, the pointer is passed by value
foobar(b) ; // fine; array decays to a pointer, the pointer is passed by value
foobaz(b) ; // fine; array decays to a pointer, the pointer is passed by value
}