# [C] Errors passing bidimensional array to a funcion

Hi I have these functions:

Code:
void posicaoIfElse (int e [][n], int i, int j, int k, int m, int n) {
........
........
}
void posicaoFormula (int e [][n], int i, int j, int k, int m, int n) {
........
........
}

that I calls through
Code:
posicaoFormula (e,i,j,k,m,n);
printf ("\n");
posicaoIfElse (a,i,j,k,m,n);

which produces together these error messages:
rede.c:3: error: 'n' undeclared here (not in a function)
rede.c:68: error: 'n' undeclared here (not in a function)
rede.c: In function 'main':
rede.c:160: error: type of formal parameter 1 is incomplete
rede.c:162: error: type of formal parameter 1 is incomplete

the funtion parameters that I am passing are:
Code:
int m = 7;
int n = m+1;
int e [n][n];
int a [n][n];
int i,j,k;

what I am doing wrong? Thanks for any help.

Last edited by a moderator:

Assuming this is C99 where variable length arrays are permitted, in short, it hasn't seen 'n' yet by the time you reference it with the array. The compiler is parsing the arguments left to right and needs to know what 'n' is in order to use it for the array. So put those variables before the array in the function definition/declaration.

That should work. Just keep in mind it's from the C99 standard, and not technically present in the C90 spec.

No it wasn't C99. Anyway I putted 'n' as the first parameter and now I am getting these two error messages:

rede.c: In function 'main':
rede.c:183: error: expected expression before ']' token
rede.c:183: warning: passing argument 3 of 'contaOcorrenciasIfElse' makes pointer from integer without a cast
rede.c:161: note: expected 'int *' but argument is of type 'int'
rede.c:184: error: expected expression before ']' token
rede.c:184: warning: passing argument 3 of 'contaOcorrenciasFormula' makes pointer from integer without a cast
rede.c:165: note: expected 'int *' but argument is of type 'int'

The lines 183 and 184 are:
Code:
contaOcorrenciasIfElse  (n,a[][n], ocorrenciasA [n], i,j);
contaOcorrenciasFormula (n,e[][n], ocorrenciasE [n], i,j);

If I pass C99 as option to the compiler, it doesn't change anything.

The lines 183 and 184 are:
Code:
contaOcorrenciasIfElse  (n,a[][n], ocorrenciasA [n], i,j);
contaOcorrenciasFormula (n,e[][n], ocorrenciasE [n], i,j);

If I pass C99 as option to the compiler, it doesn't change anything.

You don't want the [][n] parts (and later, the [n]) when you actually call the function. Just during declaration and definition of the function. If it's an nxn function, I'd put the whole thing in the declaration and definition, e.g. int blah[n][n], just to make things as explicit as possible when you declare and define, personally.

Hopefully the warnings make sense now. The first error is complaining about expecting some sort of expression between those first square brackets (which shouldn't be there anyways). The warning means it is expecting a pointer in the third parameter, but you're trying to dereference an element in that array, which would be an int and not a pointer. The compiler rightfully complains.

It's useful to know about array decaying to pointers in function calls. I'd suggest reading a large part of this C FAQ: http://www.lysator.liu.se/c/c-faq/c-2.html.

Quick fun fact, consider that array is by definition equivalent to *(array + i) (Reading the actual C99 spec is rather instructive). Which means that i[array] is equivalent to *(i + array), which works out the same since addition is commutative. They both work the same way. So accessing element 9 in an array 'x' that you'd normally want to write as x[9] will do the same thing if you write 9[x]. I'm strongly suggesting you not do that, but I just hope it gives you some insight into how they work internally.

I'll also add that pointers and arrays are not exactly the same thing. Study up on these details. The FAQ should explain a bit, I think.

Anyways, this was a little rushed (tired and need to go to bed), but someone correct me if I messed something up. It happens.

Last edited:
If it's an nxn function, I'd put the whole thing in the declaration and definition, e.g. int blah[n][n], just to make things as explicit as possible when you declare and define, personally.

What do you mean by nxn function?

Anyway, thanks for the help.

What do you mean by nxn function?

Anyway, thanks for the help.

Oops, I meant n x n array. By that, I mean if n = 5, it would be a 5 x 5 array, for example.

And you're welcome.