0

I try to pass a two dimensional array as a parameter to a function like this:

    void comb(int n, int array[n][n-1])
    {
     .....
    }

And in the main function:

    int main(int argc, const char * argv[])
    {
     const int p = 10;
     int array[p][p-1]; 
     comb(p, array); //Error:No matching function for call to 'comb'
     return 0;
    }

The "comb" function is declared above the main function. But Xcode gives me the error message on line "comb(p, array)" that "No matching function for call to 'comb' ".

I don't know how I could fix this. Also, is there some better way to pass a 2-dim array as parameter?

Biu
  • 143
  • 11

2 Answers2

1

Your code is correct in C99.

If you get a compiler error, it could be because you are not showing the real code, or you are not invoking your compiler in C99 mode.

In C11 it is optional whether the compiler supports VLA, but your compiler documentation should indicate whether or not it is supported.

There is no other way to pass a VLA as parameter.

If your array dimension is known at compile-time then you can replace const int p = 10; with #define ARRAY_DIM 10 ; then your array will no longer be a VLA and the function can simply be:

void comb(int array[ARRAY_DIM][ARRAY_DIM-1])
{
M.M
  • 130,300
  • 18
  • 171
  • 314
-2

Never passed a matrix, but when you pass arrays, the name is just the pointer to the first element.

char myArray[10];

where myArray is really a char pointer rather than a char.

You need to change something. Passing big things in functions is asking for a pointer

If comb take an array

void comb(int n, int array[n])

this should be put like

void comb(int n, int*array)

And then access to elements are in the fashion

*(array+k) ... // equals to array[k]

YOU CANNOT DO THIS

array[k] ... // Erro.

And call comb in main just in the way you did, since array names are already pointers

Not sure for bidimentional but almost sure the same, since bidim are just a big unidimensional array arranged in another way.

When passing pointer, function forget about boundaries.

Good practice is to help the function avoid disasters passing the max len

void comb(int n, int*array,int len)
{
int k=0;
While(*(array+k) !=n)    // Search array for a number equal to "n".....
{
  k++;
  if(k>len)
     return 0;     // Avoid seeking outside the array, cut now. nice plase to return 0    (not found)
}
return 1; // got a hit before reaching the end.
}

Meanwhile in the main call:

comb(p, array, sizeof(array)/sizeof(array[0]); // third param is the number of elements

  • He is passing a 2-D array, your post only talks about and demonstrates a 1-D array. Also, arrays are not "really just pointers"; and `*(array+k)` is always replaceable by `array[k]` – M.M Jul 01 '14 at 04:16
  • Of course but not sure if the user is clear in the pointer usage, so I tried to keep at the minimun. The array not a pointer, just the name, though there are some differences, pasing the name and reciving as pointer is ok and works- Guess perhaps user is not asking for programming elegance rather than solving the issue. Also, note that the stub of code I wrote can be rewritten in more tidy way, like loading all conditions in the while and incrementig the pointer itself ... just tried to make clear Cheers – user3791965 Jul 01 '14 at 11:27
  • LOL the neg. Nobody answering nor helping, just CLICKING. Well I it's wrong, you can make better, post it. Post a full answer. The member needs help, other than my post perhaps. You can mke better for sure, just share you knwolege. All have been o ARE newbies at some point. You have always something to learn and also something to teach. Regards. – user3791965 Jul 01 '14 at 11:40