C++ Multi-Dimensional Array – How to Use Multi-Dimensional Arrays

c++

I need to create a function that has a parameter which is a multi-dimensional array with two dimensions being user-specified, e.g.

int function(int a, int b, int array[a][b])
{
 ...
}

How would I do that in C++ ?

Best Answer

Are the dimensions known at compile-time? In that case, turn them into template parameters and pass the array by reference:

template<int a, int b>
int function(int(&array)[a][b])
{
    ...
}

Example client code:

int x[3][7];
function(x);

int y[6][2];
function(y);
Related Question