Multi Dimensional Arrays in C Language

Suppose that you are writing a chess-playing program. A chessboard is an 8-by-8 grid.

What data structure would you use to represent it?

You could use an array that has a chessboard-like structure, i.e. a two-dimensional array, to store the positions of the chess pieces.

Two-dimensional arrays use two indices to pinpoint an individual element of the array. This is very similar to what is called algebraic notation, commonly used in chess circles to record games and chess problems.

In principle, there is no limit to the number of subscripts (or dimensions) an array can have.

Arrays with more than one dimension are called multidimensional arrays.

While humans cannot easily visualize objects with more than three dimensions, representing multi-dimensional arrays presents no problem to computers.

In practice, however, the amount of memory in a computer tends to place limits on the size of an array. A simple four-dimensional array of double-precision numbers, merely twenty elements wide in each dimension, takes up 20^4 * 8, or 1,280,000 bytes of memory about a megabyte.

For example, you have ten rows and ten columns, for a total of 100 elements. It’s really no big deal. The first number in brackets is the number of rows, the second number in brackets is the number of columns.

So, the upper left corner of any grid would be element [0][0]. The element to its right would be [0][1], and so on. Here is a little illustration to help.

Multi Dimensional Arrays
Multi Dimensional Arrays

Three-dimensional arrays (and higher) are stored in the same way as the two-dimensional ones. They are kept in computer memory as a linear sequence of variables, and the last index is always the one that varies fastest (then the next-to-last, and so on).

MultiDimensional Array Declaration

You can declare an array of two dimensions as follows:

datatype array_name[size1][size2];

 

In the above example, variable_type is the name of some type of variable, such as int. Also, size1 and size2 are the sizes of the array’s first and second dimensions, respectively.

Here is an example of defining an 8-by-8 array of integers, similar to a chessboard.

Remember, because C arrays are zero-based, the indices on each side of the chessboard array run 0 through 7, rather than 1 through 8.

The effect is the same: a two-dimensional array of 64 elements.

int chessboard [8][8];

To pinpoint an element in this grid, simply supply the indices in both dimensions.

Initialisation of Two Dimensional Arrays

If you have an m x n array, it will have m * n elements and will require m*n element-size bytes of storage.

To allocate storage for an array you must reserve this amount of memory. The elements of a two-dimensional array are stored row wise.

If table is declared as:

int table [ 2 ] [ 3 ] = { 1,2,3,4,5,6 };

It means that element

table [ 0][0] = 1;

table [ 0][1] = 2;

table [ 0][2] = 3;

table [ 1][0] = 4;

table [ 1][1] = 5;

table [ 1][2] = 6;

The neutral order in which the initial values are assigned can be altered by including the groups in { } inside main enclosing brackets, like the following initialization as above:

int table [ 2 ] [ 3 ] = { {1,2,3},{4,5,6} };

The value within innermost braces will be assigned to those array elements whose last subscript changes most rapidly. If there are few remaining values in the row, they will be assigned zeros.

The number of values cannot exceed the defined row size.

int table [ 2 ] [ 3 ] = { { 1, 2, 3},{ 4}};

It assigns values as

table [0][0] = 1;

table [0][1] = 2;

table [0][2] = 3;

table [1][0] = 4;

table [1][1] = 0;

table [1][2] = 0 ;

Remember that, C language performs no error checking on array bounds.

If you define an array with 50 elements and you attempt to access element 50 (the 51st element), or any out of bounds index, the compiler issues no warnings. It is the programmer’s task to check that all attempts to access or write to arrays are done only at valid array indexes.

Writing or reading past the end of arrays is a common programming bug and is hard to isolate.

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: