4

How can I pass a two-dimensional array as an argument of a function (considered that the size of the array is known)? I will need help for both the declaration and the definition of the function. What I have in mind is something like this:

#include <stdio.h>
#define size 10
void function(int anarray[size][size]); //<- Is that correct?
...

void function(int anarray[][]) //<-Is this too?
{
}    

Thanks a lot!

1 Answers1

6
void function1(int anarray[size][size]); // <- Is that correct?

Yes, it is. void function1(int anarray[][size]); would work as well.

void function1(int anarray[][]) // <- Is this too?

No, that's a compiler error. Only the first (innermost) dimension of the array decays into a pointer when passed to a function.

  • could I say void function1(int anarray[N][N])? Thanks for your quick answer –  Jun 23 '13 at 20:30
  • @darkchampionz Did you read the very first line of my answer? I mean, "yes, it is" is quite enough, isn't it? –  Jun 23 '13 at 20:30
  • @darkchampionz The declaration and the defintion of the function must be compatible. Pick whichever type declaration suits you and use it consistently. –  Jun 23 '13 at 20:37