As of C99, you can declare what is known as a variable length array (VLA) where the size of the array dimension is a non-constant expression; IOW, you can do this:
int foo()
{
int x = 5;
int y = 10;
int values[x][y];
...
}
Note that this is only true for C99; C89 and earlier requires you to use compile-time constant expressions for the array dimension.
The problem with VLAs is that, because of how they work, they may only be declared at block scope (that is, within a function or a compound statement in the function); they may not be declared as static
or extern
, and they may not be declared at file scope (which is the source of your particular error message).
In this particular case, you will need to use compile-time constant expressions (which const
-qualified variables are not):
#define COLUMNS 15
#define ROWS 15
extern int board[ROWS][COLUMNS];
Note the addition of the extern
keyword in the array declaration. You don't want the declaration in the header file to be the defining declaration for the array; instead, put the defining declaration in the source file that actually implements the game board. Otherwise every source file that includes that header will try to create its own definition for board
and it will be up to the linker to sort it all out.