Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C] Errors passing bidimensional array to a funcion

  1. Oct 7, 2013 #1
    Hi I have these functions:

    Code (Text):
    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 (Text):
    posicaoFormula (e,i,j,k,m,n);
    printf ("\n");
    posicaoIfElse (a,i,j,k,m,n);
    which produces together these error messages:
    the funtion parameters that I am passing are:
    Code (Text):
    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: Oct 7, 2013
  2. jcsd
  3. Oct 7, 2013 #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.
  4. Oct 8, 2013 #3
    No it wasn't C99. Anyway I putted 'n' as the first parameter and now I am getting these two error messages:

    The lines 183 and 184 are:
    Code (Text):
    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.
  5. Oct 8, 2013 #4
    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: Oct 8, 2013
  6. Oct 8, 2013 #5
    What do you mean by nxn function?

    Anyway, thanks for the help.
  7. Oct 8, 2013 #6
    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: [C] Errors passing bidimensional array to a funcion