Two Dimensional Array

Consider an int array as declared below.

#define NUM_STUDENTS 5
#define NUM_TESTS 4
int grades[NUM_STUDENTS][NUM_TESTS];
  • By convention, the first subscript is understood to be for rows and the second for columns.
  • This array can hold up to 20 integer values, the product of the values used to declare the array.
  • As is usual in C, subscripts start with zero, so the subscripts range from 0 to 4 for the rows and 0 to 3 for the columns.
  • If the system uses four bytes for an int, this array would use 80 bytes of AM. All of the bytes are in consecutive memory locations, the first row occupying the first 20 bytes, the second the next 20, and so on.

The following table illustrates how the subscripts are specified for this array, which has five (5) rows and four (4) columns.

   

0

1

2

3

0

[0][0]

[0][1]

[0][2]

[0][3]

1

[1][0]

[1][1]

[1][2]

[1][3]

2

[2][0]

[2][1]

[2][2]

[2][3]

3

[3][0]

[3][1]

[3][2]

[3][3]

4

[4][0]

[4][1]

[4][2]

[4][3]

Back