0

Possible Duplicate:
Multi-dimensional array in C++

I need to create a function that has one 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++ ?

Community
  • 1
  • 1
Gonenc
  • 197
  • 4

2 Answers2

2

You can do something like this with a template:

template <int A, int B>
int function(int array[A][B])
{
 ...
}

Note that this generates a block of code for each size of array that you use. You can optimise slightly by passing in the outer dimension as a function parameter instead of a template parameter (you can't change the inner dimension because you can't have an array of an array of unknown size).

EDIT: As pointed out on the duplicate bug, I got my template array syntax wrong.

Neil
  • 54,642
  • 8
  • 60
  • 72
0

You have to pass size with parameters.
int function(int a, int b, int array[][])
There is no length() method or a method like that in C++. If you pass an array, it's just a link to that. This is the same as int function(int a, int b, int** array)
EDIT:
Note that if you use square braces, not asterisks, you have to give size of all dimensions except the first one. Actually you're passing array of arrays and compiler must know size of each of them.

shift66
  • 11,760
  • 13
  • 50
  • 83