Two Dimensional Arrays in C Programming

Two Dimensional Array

Two-dimensional array are those type of array, which has finite number of rows and finite number of columns. The declaration form of 2-dimensional array is

Data_type Array_name [row size][column size];

The type may be any valid type supported by C. The rule for giving the array name is same as the ordinary variable. The row size and column size should be an individual constant.

The following declares a two-dimensional 3 by 3 array of integers and sets the first and last elements to be 10.
int matrix [3][3];
matrix[0][0] = 10;
matrix[2][2] = 10;

The following Figure illustrates a two dimensional array, matrix. The array contains three rows and tree columns, so it is said to be a 3-by-3 array. In general, an array with m rows and n columns is called an m-by-n array.

[0] [1] [2]
[0] 10
[1]
[2] 10

Every element in array matrix is identified by an element name of the form matrix[ i ][ j ]; matrix is the name of the array, and i and j are the subscripts that uniquely identify each element in matrix . Notice that the names of the elements in the first row all have a first subscript of 0; the names of the elements in the third column all have a second subscript of 2.

In the case of Two-dimensional array, during declaration the maximum number of rows and maximum number of column should be specified for processing all array elements.

The implementation of the array stores all the elements in a single contiguous block of memory. The other possible implementation would be a combination of several distinct one-dimensional arrays. That's not how C does it. In memory, the array is arranged with the elements of the rightmost index next to each other. In other words, matrix[1][1] comes right before matrix[1][2] in memory.

The following array:

[0] [1] [2]
[0] 1 2 3
[1] 4 5 6
[2] 7 8 9

would be stored:

1 2 3 4 5 6 7 8 9

Example:

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!