[C] Errors passing bidimensional array to a funcion

  • Thread starter Thread starter colt
  • Start date Start date
  • Tags Tags
    Array Errors
AI Thread Summary
The discussion centers on errors encountered when passing a bidimensional array to functions in C. The primary issue arises from the undeclared variable 'n' at the time of array definition, which leads to incomplete type errors. It is suggested that 'n' should be defined before being used in the array declaration. Additionally, when calling functions, the syntax should not include the array dimensions, as this leads to further type mismatches and warnings. Understanding array decay to pointers and the distinction between arrays and pointers is emphasized as crucial for resolving these issues.
colt
Messages
20
Reaction score
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
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.
 
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:
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top