The types of arrays that you have been exposed to so far are all linear arrays—that is, they all dealt with a single dimension. The C language allows arrays of any dimension to be defined. In this section, you take a look at two-dimensional arrays.
One of the most natural applications for a two-dimensional array arises in the case of a matrix. Consider the 4 x 5 matrix shown in Table 7.2.
In mathematics, it is quite common to refer to an element of a matrix by use of a double subscript. So if you call the preceding matrix M, the notation Mi,j refers to the element in the ith row, jth column, where i ranges from 1 to 4, and j ranges from 1 to 5.
The notation M3,2 refers to the value 20, which is found in the 3rd row, 2nd column of the matrix. In a similar fashion, M4,5 refers to the element contained in the 4th row, 5th column: the value 6.
In C, you can use an analogous notation when referring to elements of a two- dimensional array. However, because C likes to start numbering things at zero, the 1st row of the matrix is actually row 0, and the 1st column of the matrix is column 0. The preceding matrix would then have row and column designations, as shown in Table 7.3.
Whereas in mathematics the notation Mi,j is used, in C the equivalent notation is
M[i][j]
Remember, the first index number refers to the row number, whereas the second index number references the column. So the statement
sum = M[0][2] + M[2][4];
adds the value contained in row 0, column 2—which is –3—to the value contained in row 2, column 4—which is 14—and assigns the result of 11 to the variable sum.
Two-dimensional arrays are declared the same way that one-dimensional arrays are;
thus
int M[4][5];
declares the array M to be a two-dimensional array consisting of 4 rows and 5 columns, for a total of 20 elements. Each position in the array is defined to contain an integer value.
Two-dimensional arrays can be initialized in a manner analogous to their one- dimensional counterparts. When listing elements for initialization, the values are listed by row. Brace pairs are used to separate the list of initializers for one row from the next. So to define and initialize the array M to the elements listed in Table 7.3, a statement such as the following can be used:
int M[4][5] = {
{ 10, 5, -3, 17, 82 },
{ 9, 0, 0, 8, -7 },
{ 32, 20, 1, 0, 14 },
{ 0, 0, 8, 7, 6 }
};
Pay particular attention to the syntax of the preceding statement. Note that commas are required after each brace that closes off a row, except in the case of the final row. The use of the inner pairs of braces is actually optional. If not supplied, initialization proceeds by row. Thus, the preceding statement could also have been written as follows:
int M[4][5] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7, 32,
20, 1, 0, 14, 0, 0, 8, 7, 6 };
As with one-dimensional arrays, it is not required that the entire array be initialized. A statement such as
int M[4][5] = {
{ 10, 5, -3 },
{ 9, 0, 0 },
{ 32, 20, 1 },
{ 0, 0, 8 }
};
only initializes the first three elements of each row of the matrix to the indicated values. The remaining values are set to 0. Note that, in this case, the inner pairs of braces are required to force the correct initialization. Without them, the first two rows and the first two elements of the 3rd row would have been initialized instead. (Verify to yourself that this is the case.)
Subscripts can also be used in the initialization list, in a like manner to single- dimensional arrays. So the declaration
int matrix[4][3] = { [0][0] = 1, [1][1] = 5, [2][2] = 9 };
initializes the three indicated elements of matrix to the specified values. The unspecified elements are set to zero by default.
Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.