When an array expression appears in most contexts, its type is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to the address of the first element in the array. The exceptions to this rule are when the array expression is an operand of the sizeof
or address-of (&
) operators, or if the array expression is a string literal being used to initialize another array in a declaration.
What this means in the context of your code is that in your call to box_sort
, the type of the expression boxes
is implicitly converted from M-element array of N-element array of int
to pointer to N-element array of int
, or int (*)[MAX_DIMENSIONALITY+1]
, so your function should be expecting parameter types like:
void box_sort(int (*arr)[MAX_DIMENSIONALITY+1], int x, int y)
{
...
}
Since int *a
and int a[]
are synonymous in a function parameter declaration, it follows that int (*a)[N]
is synonymous with int a[][N]
, so you could write the above as
void box_sort(int arr[][MAX_DIMENSIONALITY+1], int x, int y)
{
}
although I personally prefer the pointer notation, as it more accurately reflects what's going on. Note that in your function, you would subscript arr
as normal:
arr[x][y] = ...;
since the expression arr[x]
is equivalent to *(arr + x)
, the pointer is implicitly dereferenced.
If you want box_sort to work on arbitrarily-sized arrays (i.e., arrays where the second dimension isn't necessarily MAX_DIMENSIONALITY+1), then one approach is to do the following:
int boxes[X][Y];
...
box_sort (&boxes[0], X, Y, x, y);
...
void box_sort(int *arr, size_t rows, size_t cols, int x, int y)
{
...
arr[x*cols + y] = ...;
}
Basically, you're treating boxes
as a 1-d array of int and calculating the offsets manually.