Reputation: 1436
I have the following code:
void getPossibilities(int *rating[200][3]){
// do something
}
int main ()
{
int rating[200][3];
getPossibilities(&rating);
}
this throws following error message:
error: cannot convert int ()[200][3] to int ()[3] for argument 1 to void getPossibilities(int (*)[3])
Upvotes: 2
Views: 2163
Reputation: 361254
The function signature should be this:
void getPossibilities(int (*rating)[3]);
and pass argument as:
getPossibilities(rating);
The variable rating
is a two dimentional array of form T[M][N]
which can decay into a type which is of form T(*)[N]
. So I think that is all you want.
As in the above solution the array decays, losing the size of one dimension (in the function you only know N
reliably, you just loss M
due to the array-decay), so you've to change the signature of the function to avoid decaying of the array:
void getPossibilities(int (&rating)[200][3]) //note : &, 200, 3
{
//your code
}
//pass argument as before
getPossibilities(rating); //same as above
Better yet is to use template as:
template<size_t M, size_t N>
void getPossibilities(int (&rating)[M][N])
{
//you can use M and N here
//for example
for(size_t i = 0 ; i < M ; ++i)
{
for(size_t j = 0 ; j < N ; ++j)
{
//use rating[i][j]
}
}
}
To use this function, you've to pass the argument as before:
getPossibilities(rating); //same as before!
Upvotes: 5
Reputation: 69958
When passing an array of N-dimensions to a function, the 0th dimension is always ignored. That's why a[N]
decays to *p
. In the same way, a[N][M]
decays to (*p)[M]
.
Here, (*p)[M]
is a pointer to an array of M
elements.
int a1[N][M], a2[M];
int (*p)[M];
p = a1; // array a1[N][M] decays to a pointer
p = &a2; // p is a pointer to int[M]
So your function signature should be:
void getPossibilities(int (*rating)[3]);
Now since you are using, C++, it's worth taking advantage of its facility where you can pass an array by reference. So preferred way is:
void getPossibilities(int (&rating)[200][3]);
Upvotes: 0
Reputation: 36423
There is a difference between int (*x)[200][3]
and int *x[200][3]
Upvotes: 0