Search code examples
carraysconstants

C declaring a two-dimensional array using constants


I have a header file for a game that declares a two-dimensional array for a board.

#ifndef GAME_H_
#define GAME_H_

static const int columns = 15;
static const int rows = 15;

int board[rows][columns];

#endif /* GAME_H_ */

I get an error:

error: variably modified 'board' at file scope


Solution

  • C doesn't allow const variables as array bounds. Use an enum instead:

    enum { columns = 15, rows = 15 };