2

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])

3 Answers 3

5

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!
Sign up to request clarification or add additional context in comments.

4 Comments

but I need a two dimensional array, in my function I use something like this: *rating[i][0] = *rating[i][0] +1; then the error is error: invalid type argument of unary '*' (have 'int')
The function signature should be strongly typed.
@DeadMG: Isn't it strongly-typed?
@Nawaz My problem now is that if I parse rating without the *, that the value is not changed when the function is finished, or is it?
0

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]);

Comments

0

There is a difference between int (*x)[200][3] and int *x[200][3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.