If you have a C99 or C11 compiler available, then you can pass both dimensions of the array as arguments before the array parameter, as shown in ThoAppelsin's answer.
If you are stuck with a C89 compiler, then you have to work harder — much harder. By far the simplest solution is to make the #define
dimensions available in the header; then you can write:
int function(grid[HEIGHT][WIDTH]) { ... }
int function(grid[][WIDTH]) { ... }
If you can't do that, then you'll have to think about modifying either the code in the function or the data structures in main.c
.
Modifying the function means:
int function(int height, int width, int *grid)
{
int i, j;
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
grid[i*width + j] = 0;
return j;
}
This takes an ordinary pointer-to-int
and does the index calculations explicitly, rather than simply writing two subscripts. Your main code might have:
int grid[HEIGHT][WIDTH];
i = function(HEIGHT, WIDTH, &grid[0][0]);
The alternative is that you create a secondary structure, a set of pointers to pointers, in main()
:
int grid_rows[HEIGHT];
for (i = 0; i < HEIGHT; i++)
grid_row[i] = grid[i];
i = function(HEIGHT, WIDTH, grid_rows);
Now the definition is:
int function(int height, int width, int **grid)
{
int i, j;
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
grid[i][j] = 0;
return j;
}
This reinstates the notational convenience in the function at the cost of the extra storage and initialization.