function call and function prototype in c with a 2d array parameter

D_Man picture D_Man · Feb 26, 2013 · Viewed 16.5k times · Source
void setup_map (int *map); <--- prototype

int row, col;  <-- global variables

some main 
{
  //get number of rows and cols from user
  int map[rows][cols]; //create map here because another function uses it
  setup_map (map[row][col]);
}

void setup_map (int map[row][col])
{
  loop through and set up map
}

my problem is I cant get the prototype quite right I was hoping somewhere could explain to me what my prototype needs to be? I have just begun learning about pointers and understand the concept pretty well just haven't ever used a 2d array as a argument. Thanks for any help.

Answer

user529758 picture user529758 · Feb 26, 2013

Correct prototypes include:

void setup_map(int map[ROWS][COLS]);
void setup_map(int map[][COLS]);
void setup_map(int (*map)[COLS]);

And to call it:

setup_map(map);

Note that, however, that the number of rows and columns needs to be a compile-time constant for this to work.