We have entered a new dimension. Or perhaps several! The only benefit (or drawback) is that we won't be leaving Earth; instead we will be adding complexity and depth to our arrays in C.
A multi-dimensional array is an array that has more than one dimension. It is an array of arrays; an array that has multiple levels. The simplest multi-dimensional array is the 2D array, or two-dimensional array. It's technically an array of arrays, as you will see in the code. A 2D array is also called a matrix, or a table of rows and columns.
Declaring a multi-dimensional array is similar to the one-dimensional arrays. For a 2D array, we need to tell C that we have 2 dimensions.
This example declares a 2D integer array:
int two_d[3][3];
A valid type is required (in this case int), followed by the name of the array, and the size of each dimension. In this case, we created a 2D array that's 3 by 3 (three rows and three columns). As stored in the computer's memory, the array looks like this table. Remember that C starts counting all buckets at 0!
Column 0 Column 1 Column 2
Row 0 two_d[0][0] two_d[0][1] two_d[0][2]
Row 1 two_d[1][0] two_d[1][1] two_d[1][2]
Row 2 two_d[2][0] two_d[2][1] two_d[2][2]
As mentioned before, the 2D array is a matrix. Therefore, it's well-suited for spreadsheet-type data (e.g., period table, list of employees and their attributes, lab test results, etc.).
For example, we could create a 2D array for 50 students to capture scores on four tests:
int gradebook[50][4];