2D Arrays in C language – How to declare, initialize and access elements

We already know that arrays are a collection of the same type of data that have a fixed size(in C programming language as in other languages we can increase the size of an array at runtime).
Arrays can also be classified based on their dimensions, like:

In this tutorial, we will learn more about the 2D array. A 1-D array, as we saw in the previous tutorial, is a linear list of data. The two-dimensional arrays are also known as matrix. Similar to a one-dimensional array, in a two-dimensional array, we have the same name for all the elements present in the matrix. The difference that we have here is that a two-dimensional array is not linear in nature. It is like a matrix and has a row and a column of elements ( Although in memory these are stored in continuous memory locations). The following figure illustrates the difference between a one-dimensional array and a two-dimensional array:

2d-arrays

In the figure, we can clearly see that the 2D array has two dimensions just like any two-dimensional figure like a square or a rectangle. Also, the number of rows and the number of columns in the 2D array are represented by this format:

ArrayVariableName[number of rows] [number of columns]

For the above 2D array we have the number of rows=3 and number of columns=3, so we represent it as a 3×3 array or a 3×3 matrix. A matrix can also have specifications like 3×4,2×1, etc. Let us learn more about the 2D arrays.


Declaring an Array

A 2D array needs to be declared so that the compiler gets to know what type of data is being stored in the array.

Similar to 1D array, a 2D array can also be declared as an int, char, float, double, etc. Here is how we declare a 2D array(here integer array):

datatype arrayVariableName[number of rows] [number of columns]

int num[10][5];

The ‘int’ specifies that the data stored in the array will be of integer type.
‘num’ is the variable name under which all the data is stored.
[10] refers to the number of rows of the array and
[5] refers to the number of columns of the array.

This is also a static memory allocation, that is, we are allocating the array a size equal to 10×5 , that is, in this array, we can store 10×5=50 number of elements. the actual size it will occupy in memory will depend on the datatype of the array i.e. (number of elements it can hold x Size of datatype).
The two [][] brackets specifies that the array is two dimensional.


Initializing and storing data in an array

2D array initialization can be done during the declaration of the array as well. In simple words, we are storing certain elements in the array while writing the program i.e. we know which values this array will always store. Here are a few examples of initializing a 2D array:

int num[3][3]={{25,10,5},{4,6,13},{45,90,78}};//or the format given below
int num[3][3]={{25,10,5}, //row0
               {4,6,13},//row1
               {45,90,78}//row2
              };
int num[2][4]={4,6,8,10,3,5,7,9};//this format also stores data in the array but results in a loss of readability

Another important fact is that when initializing a 2D array, specifying the number of rows of the array is optional but specifying the number of columns of the array is important and mandatory. Like 1D array, the user can also input the values in the array using a for loop. The only difference is that we use a nested for loop for inserting the elements in the array. There are two ways of insertion: row-wise insertion and column-wise insertion. The typical way of insertion is row-wise insertion. Here is an example of row-wise insertion format(for a 3×3 matrix):

for(int i=0 ;i < 3 ;i++){
  for(int j=0 ;j < 3 ;j++){
    scanf("%d",&num[i][j]);
}}

Accessing and Reading the array

In case of 2D arrays, we use index numbers(like 1D arrays) in the subscript to access the array elements. The outer loop indicates the row index and the inner loop indicates the column index. The following figure shows how the array elements are indexed:

2d-array-indexing

We already see that the index format is [row number][column number]. Suppose we need to access the element in row 1 and column 2, we just need to write arrayName[1][2].In order to access all the array elements we use nested for loops. Here is the syntax to access all the array elements in a matrix format(for a 3×3 matrix):

for(int i=0 ;i < 3 ;i++){
  for(int j=0 ;j < 3 ;j++){
     printf("%d",&num[i][j]);
  }printf("\n");
}

Program to initialize 2D array with User input and print it

Here is a simple program of 2D array which adds two arrays and stores the result in another array. One array has already been initialized and the other one will have data input by the user.


#include <stdio.h>
int main()
{
    //we initialize the first array and the second array will have user input values
    int a[3][3]={{1,34,5},{7,0,15},{23,4,6}};//first array initialization
    int b[3][3],c[3][3],i,j;
    
    printf("Enter values in the 3x3 array:\n");
    
    for(i=0 ;i<3 ;i++){ //outer loop for rows
        for(j=0 ;j<3 ;j++){  //inner loop for columns
            scanf("%d",&b[i][j]);
            c[i][j]=a[i][j]+b[i][j];//summing up the values of the two arrays
        }
    }
    
    //displaying the array elements after summing up
    for(i=0 ;i<3 ;i++){
        for(j=0 ;j<3 ;j++){
                printf("%d  ",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}


Output:-
Enter values in the 3x3 array:
30
4
17
6
9
14
20
0
6

31  38  22
13  9  29
43  4  12

So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Do not forget to SHARE and SUBSCRIBE.

Keep Coding!! Happy Coding!! 🙂

Recommended -