[C] Errors passing bidimensional array to a funcion

Just remember to study up on pointers and arrays, as they are not exactly the same thing. The FAQ should help with that.
  • #1
colt
22
0
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:
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
colt said:
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:
  • #5
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.
 
  • #6
colt said:
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.
 

FAQ: [C] Errors passing bidimensional array to a funcion

Why am I getting an error when trying to pass a bidimensional array to a function?

When passing a bidimensional array to a function, it is important to remember that you are passing a pointer to the first element of the array. This means that the function expects a single-dimensional array, not a bidimensional one. So if you try to pass a bidimensional array, the function will not be able to interpret the data correctly and will throw an error.

How can I fix the error when passing a bidimensional array to a function?

To fix this error, you can pass the bidimensional array as a pointer to a pointer. This means that the function will receive a pointer to the first row of the array, and each element of this row will also be a pointer to the first element of each column. This way, the function can correctly interpret the data in the bidimensional array.

Can I pass a bidimensional array as a parameter to a function without using a pointer?

No, it is not possible to pass a bidimensional array as a parameter to a function without using a pointer. This is because arrays in C are always passed by reference, which means that the function receives a pointer to the first element of the array. So even if you try to pass a bidimensional array directly, without using a pointer, the function will still receive a pointer.

Why do I need to use a pointer when passing a bidimensional array to a function?

Using a pointer when passing a bidimensional array to a function allows for a more efficient use of memory. If you were to pass a bidimensional array directly, the entire array would need to be copied into the function's stack, which can be a costly operation for large arrays. By using a pointer, you are only passing the address of the first element, which is more efficient and avoids unnecessary memory usage.

Is it possible to modify a bidimensional array passed as a parameter to a function?

Yes, it is possible to modify a bidimensional array passed as a parameter to a function. This is because, as mentioned before, arrays in C are always passed by reference. This means that any changes made to the array inside the function will also be reflected in the original array passed as a parameter. However, if you want to avoid modifying the original array, you can make a copy of it and pass the copy to the function instead.

Similar threads

Replies
25
Views
2K
Replies
22
Views
3K
Replies
23
Views
2K
Replies
39
Views
4K
Replies
4
Views
2K
Replies
22
Views
3K
Back
Top