Two Dimensional Array in C

Two-dimensional Array

The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare and access elements of a 2-D array we use 2 subscripts instead of 1.

Syntax: datatype array_name[ROW][COL];

The total number of elements in a 2-D array is ROW*COL. Let’s take an example.

This array can store 2*3=6 elements. You can visualize this 2-D array as a matrix of 2 rows and 3 columns.

conceptual-representation-of-2d-array

The individual elements of the above array can be accessed by using two subscript instead of one. The first subscript denotes row number and second denotes column number. As we can see in the above image both rows and columns are indexed from 0. So the first element of this array is at arr[0][0] and the last element is at arr[1][2]. Here are how you can access all the other elements:

arr[0][0] – refers to the first element
arr[0][1] – refers to the second element
arr[0][2] – refers to the third element
arr[1][0] – refers to the fourth element
arr[1][1] – refers to the fifth element
arr[1][2] – refers to the sixth element

If you try to access an element beyond valid ROW and COL , C compiler will not display any kind of error message, instead, a garbage value will be printed. It is the responsibility of the programmer to handle the bounds.

arr[1][3] – a garbage value will be printed, because the last valid index of COL is 2
arr[2][3] – a garbage value will be printed, because the last valid index of ROW and COL is 1 and 2 respectively

Just like 1-D arrays, we can only also use constants and symbolic constants to specify the size of a 2-D array.

Processing elements of a 2-D array

To process elements of a 2-D array, we use two nested loop. The outer for loop to loop through all the rows and inner for loop to loop through all the columns. The following program will clear everything.

Expected Output:

How it works:

There is nothing new in this previous program that deserves any explanation. We are just using two nested for loops. The first nested for loop takes input from the user. And the second for loop prints the elements of a 2-D array like a matrix.

Initializing 2-D array

Initialization of 2-D array is similar to a 1-D array. For e.g:

2d-array-notation

After this initialization, each element is as follows:

Consider another initialization.

The size of my_arr is 4*3=12 , but in the initialization, we have only specified the value of 8 elements. In such cases, the remaining elements will be given the value of 0.

The individual elements are as follows:

In 2-D arrays, it is optional to specify the first dimension but the second dimension must always be present. This works only when you are declaring and initializing the array at the same time. For example:

is same as

As discussed earlier you can visualize a 2-D array as a matrix. The following program demonstrates the addition of two matrices.

Expected Output:

How it works:

Two matrices can be added or subtracted, only if they have the same dimension. In other words, a matrix of size 2*3 can be added to another matrix of 2*3, but you can’t add or subtract it to a matrix of 2*4 or 3*2. The resultant array will be a matrix of the same dimension as the original two. First two for loops asks the user to enter two matrices. The third for loop adds corresponding elements of mat1 and mat2 in a new array mat3. Fourth for loop prints the elements of array mat3.

Arrays of more than two dimension

You can even create an array of 3 or more dimensions or more, but generally, you will never need to do so. Therefore, we will restrict ourself to 3-D arrays only.

Here is how you can declare an array of 3 dimensions.

3-D array uses three indexes or subscript. This array can store 2*3*2=12 elements.

Here is how to initialize a 3-D array.

You can think of this array as 2 2-D arrays and each of these 2-D array has 3 rows and 4 columns;

Here are individual elements of the array:

First Row

Second Row

Passing Multidimensional Arrays to Functions

You can pass multi-dimensional arrays to functions just like a 1-D array, but you need to specify the size of the all other dimensions except the first one. For e.g:

If you need to pass arr[2][3] to a function called func_1(), then you need to declare the func_1() like this:

or like this:

It would be invalid to declare formal argument as follows:

Similarly to pass a 3-D array you need to declare the function as follows:

3 thoughts on “Two Dimensional Array in C

Leave a Comment