< Previous | Contents | Next >

Creating Multidimensional Arrays

One of the first things I do in the program is declare and initialize an array for the tic-tac-toe board.

char board[ROWS][COLUMNS] = { {’O’, ’X’, ’O’},

{’ ’, ’X’, ’X’},

{’X’, ’O’, ’O’} };

The preceding code declares a 3 3 (since ROWS and COLUMNS are both 3) two- dimensional character array. It also initializes all of the elements.


Hin t

image

It’s possible to simply declare a multidimensional array without initializing it. Here’s an example:

char chessBoard[8][8];

The preceding code declares an 8 8, two-dimensional character array, chessBoard. By the way, multidimensional arrays aren’t required to have the same size for each dimension. The following is a perfectly valid declaration for a game map represented by individual characters:

char map[12][20];

image