A function that takes a 2D-array as an argument may have the following prototype
void foo(int arr[][<cols>]);
where <cols>
is the number of columns in the array to be passed. For example
void foo(int arr[][3]);
declares a function foo
that takes a 2D int
array arr
that has 3 columns and any number of rows in it.
As the error message shows, your function expects an int *
while you are trying to pass a value of type int (*)[3][2]
. These are two different types. The former is a pointer to an int
while the latter is a pointer to 2D-array of int
s (i.e., the type of &foo
).
The following piece of code passes a 2D-array to foo
(declared above)
int bar[3][3];
foo(bar);
Notice that I'm not passing &bar
because that would be a pointer to my 2D-array not my 2D-array itself.
Accessing the elements of the array within foo
would be as follows
arr[0][0]; // accesses the element in the zeroth row and the zeroth column
If you want your function to take a pointer to a 2D-array of int
s, it may have the following prototype
void baz(int (*arr_ptr)[][<cols>]);
where <cols>
is the number of columns that are in the array that is pointed to by arr_ptr
. For example
void baz(int (*arr_ptr)[][3]);
declares a function baz
that takes a pointer to a 2D int
array arr_ptr
as an argument.
The following code passes a pointer to a 2D int
array to baz
int bar[3][3];
baz(&bar);
Accessing the elements of the array within baz
would be as follows
(*arr_ptr)[0][0]; // accesses the element at the zeroth row and the zeroth column
Notice that I had to dereference arr_ptr
first to get the array back, then I accessed the element at [0][0].