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

In this C programming tutorial, we will discuss how to declare, initialize, access, and iterate over two-dimensional arrays and implement a program using 2D arrays.

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 or two-dimensional arrays.

1. What is a 2D array in C?

A 2D array is like a matrix and has a row and a column of elements ( Although in memory these are stored in contiguous memory locations).

A 1-D array, as we saw in the previous tutorial, is a linear list of data and needed only one index to access the element like a[2]. To access a two-dimensional array we need a pair of indices one for the row and another for the column like a[1][2].

Similar to a one-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.

The following figure illustrates the difference between a one-dimensional array and a two-dimensional array:

2d-arrays
1D array vs 2D array in C

In the above figure, we can clearly see that the 2D array has two dimensions just like any two-dimensional figure such as a square or a rectangle.

Also, the number of rows and the number of columns in the 2D array are represented by this format:

2D array 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.


2. How to declare a 2D Array in C?

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):

2D array declaration

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 x 5, that is, in this array, we can store 10 x 5 = 50 number of elements.

The actual size it will occupy in memory will depend on the data type of the array i.e. (size of array = number of elements it can hold x Size of datatype).

The two brackets i.e. [][] specify that the array is two-dimensional.


3. How to initialize and store data in a 2D array in C?

2D array initialization can be done while declaring the array as well. In simple words, we can store 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:

//or the format given below
int num[3][3]={{25,10,5},{4,6,13},{45,90,78}};

int num[3][3]={{25,10,5}, //row0
               {4,6,13},  //row1
               {45,90,78} //row2
              };

//this format also stores data in the array but results in a loss of readability
int num[2][4]={4,6,8,10,3,5,7,9};

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 a 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 a 2D 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]);
  }
}

4. How to access and read data in a 2D array in C?

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
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");
}

5. 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

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anush
1 year ago

Thnks for information

1
0
Would love your thoughts, please comment.x
()
x
Index