Multidimensional Arrays
So far, all the arrays you've seen have been one-dimensional arrays, in which the dimension sizes are placed within a pair of brackets ([ and ]).
In addition to one-dimensional arrays, the C language also supports multidimensional arrays. You can declare arrays with as many dimensions as your compiler allows.
The general form of declaring a N-dimensional array is
data-type Array-Name[Array-Size1][Array-Size2]. . .
[Array-SizeN];
where N can be any positive integer.
Because the two-dimensional array, which is widely used, is the simplest form of the multidimensional array, let's focus on two-dimensional arrays in this section. Anything you learn from this section can be applied to arrays of more than two dimensions, however.
For example, the following statement declares a two-dimensional integer array:int array_int[2][3];
Here there are two pairs of brackets that represent two dimensions with a size of 2 and 3 integer elements, respectively.
You can initialize the two-dimensional array array_int in the following way:
TYPE
array_int[0][0] = 1;
array_int[0][1] = 2;
array_int[0][2] = 3;
array_int[1][0] = 4;
array_int[1][1] = 5;
array_int[1][2] = 6;
which is equivalent to the statement
int array_int[2][3] = {1, 2, 3, 4, 5, 6};
Also, you can initialize the array_int array in the following way:
int array_int[2][3] = {{1, 2, 3}, {4, 5, 6}};
Note that array_int[0][0] is the first element in the two-dimensional array array_int; array_int[0][1] is the second element in the array; array_int[0][2] is the third element; array_int[1][0] is the fourth element; array_int[1][1] is the fifth element; and array_int[1][2] is the sixth element in the array.
The program in Listing 12.6 shows a two-dimensional integer array that is initialized and printed out on the screen.
Listing 12.6. Printing out a two-dimensional array.
OUTPUT1: /* 12L06.c: Printing out a 2-D array */
2: #include <stdio.h>
3:
4: main()
5: {
6: int two_dim[3][5] = {1, 2, 3, 4, 5,
7: 10, 20, 30, 40, 50,
8: 100, 200, 300, 400, 500};
9: int i, j;
10:
11: for (i=0; i<3; i++){
12: printf("\n");
13: for (j=0; j<5; j++)
14: printf("%6d", two_dim[i][j]);
15: }
16: return 0;
17: }
ANALYSIS
The following output is obtained by running the executable 12L06.exe:
C:\app>12L06
1 2 3 4 5
10 20 30 40 50
100 200 300 400 500
C:\app>
As you can see in Listing 12.6, there is a two-dimensional integer array, two_dim, declared and initialized in lines 6_8.
In lines 11_15, two for loops are nested together. The outer for loop increments the integer variable i and prints out the newline character \n in each iteration. Here the integer variable i is used as the index to the first dimension of the array, two_dim.
The inner for loop in lines 13 and 14 prints out each element, represented by the two_dim[i][j] expression, by incrementing the index to the second dimension of the array. Therefore, I obtain output like the following
1 2 3 4 5
10 20 30 40 50
100 200 300 400 500
after the two nested for loops are run successfully.
- 6 views