0

I have a problem with an array/pointer as an input parameter.

I want to use:

int A(int ncontours, int cntr[], double* vertices[], int *triangles [3])
{
...
B(n, nmonpoly, triangles);
...
}

A is defined in an h-file as:

extern int A(int, int *, double (*)[2], int (*)[3]);

B is defined as

extern int B(int, int, int (*)[3])

The error message is:

Error 1 error C2664: 'B' : cannot convert parameter 3 from 'int []' to 'int ()[3]'

I want to correct the input parameters of A to make them match the call of B.

I guess the problem is that it's a pointer to an array?

How can this be done?

Thanks!

4
  • int *triangles [3] and int(*)[3] are not the same, or wasn't the error message clear on that? The former is an array of pointers (as a param, the subscript is useless), the latter is a pointer to an array of three int, and the subscript is very meaningful. Commented Apr 13, 2014 at 20:41
  • yes I understood that, but Im struggling with writing the function parameter list in the implementation. I tried various combinations of (*) and [3] but none seems to work Commented Apr 13, 2014 at 20:43
  • You're missing my point: int A(int ncontours, int cntr[], double* vertices[], int *triangles [3]) is in the definition of the function, while the same function is extern as extern int A(int, int *, double (*)[2], int (*)[3]);. The decl of A in the header doesn't match the definition of A in he source file. They're not the same. Neither the vertices nor the triangles match by-type. Fix the definition to match the decl. Commented Apr 13, 2014 at 20:46
  • If I change it to A(int, int , double ()[2], int (*)[3]); then Triangles is unknown. How can that be solved? Commented Apr 13, 2014 at 21:10

2 Answers 2

3
int (*)[3]

means: give me a pointer to an array of 3 integers

int *triangles [3]

means: I'm receiving an array of 3 pointers to integer.

Those are two completely different things. The precedence of the symbols in those expressions matter, that's the reason the () parenthesis are in place.

It seems also that the definition of A is wrong, it's a totally different function due to the difference I posted above. If you fix A's definition, you'll likely be able to pass the triangles parameter to B without problems since their types will match.

Sign up to request clarification or add additional context in comments.

3 Comments

it's only about A, I assume that B is correct but A is not. If I copy A's definition to the implementation like ..., int (*)[3]), then the compiler error disappears but triangles doesn't exist anymore
int (*triangles)[3] looked promising to me, but it's useless
try to define A as int A(int ncontours, int *cntr, double *vertices, int *triangles); notice that you might lose the array dimensions due to array decaying unless you pass it by reference
0

A should be:

int A(int ncontours, int cntr[], double* vertices[], int (*triangles) [3])

You should be getting a compilation error that the definition and declaration of A didn't match.

Also, can you show the code that calls A? Another option would be to make A and B both take int *[3] instead of int (*)[3]; the point is that they both have to be the same.

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.