0

Possible Duplicate:
Passing multidimensional arrays as function arguments in C

In C, if I want a function to receive a 2-D array, can I use * notation for the function parameter

int (int my2dary[][10]);  //This is what I do not want.
Community
  • 1
  • 1
SystemFun
  • 1,062
  • 4
  • 11
  • 21

2 Answers2

0

Yes, you pass a pointer to an array of int

int func(int (*my2dary)[10]);

and you call it

int a[5][10];
func(a);

Although, func doesn't know how many elements are in my2dary, so you must give a number too

int func(int n, int (*my2dary)[10]);

and call

int a[5][10];
func(5, a);

See How to interpret complex C/C++ declarations or The ``Clockwise/Spiral Rule''.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

If your problem is that you don’t know the size of the array at compile time, you may want:

int func(int *array, int size)
{
   int n,m;
   ... 
   array[m*size+n]; /* = array[m][n] if in the caller: int array[x][size]; */
}

Optionally (and very probably you need) you can pass a second size argument (x) to be able to test array boundary

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32