Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Learn More

Article

Codzify - Two dimensional (2D) arrays in C programming with example

3 min 42 sec read

As we discussed in our previous article on One Dimensional array, data was organized linearly in only one direction.

int a[5] = {1,2,3,4,5};

Two Dimensional array provides the way to store data in the form of grids and tables. You can think of the two-dimensional array as an array of one-dimensional array.

 

From the figure, you might notice that there are three one dimensional arrays in three rows.

 

Syntax:

datatype array_Name[row_size][column_size]

Two Dimensional arrays are specified using two subscripts where the first subscript denotes the row and the second denotes the column.

Suppose, you want to store marks of two students in three subjects then two-dimensional array can be declared like this,

marks[2][3]

Initializing Two-dimensional Arrays

2d arrays are initialized using the same syntax as one dimensional array,

int marks[2][3] = {88,87,76,88,76,56}

As you see, for marks[2][3] dimensional array there are 6 elements . In generalize term, we can say for arr[m][n] dimensional array, there will be m X n number of elements in an array.

You can think about the given initialized array of marks like this also,

int marks[2][3] = {{88,87,76}, {88,76,56}}

Initialize the entire two dimensional array to zeros,

 int marks[2][3] = 0;

The individual elements of two dimensional array can be initialized using the assignment operator,

marks[0][1] = 87;

Simple program to understand the Two dimensional array in action

Program to print the elements of 2d array in C,

#include <stdio.h>
int main()
{
   int arr[2][2] = {10,11,12,13};
   int i,j;
   for(i = 0; i<2; i++)
   {
     printf("
");
     for(j = 0; j<2; j++)
     {
       printf("%d	", arr[i][j]);
     }
   }
return 0;
}
}

Output

10   11
12   13

How two dimensional arrays are stored in memory ?

There are two ways of storing the two-dimensional array in memory.

  • Row major order
  • Column major order

Row-major order

The elements in row-major order are stored row by row in memory. Elements of the first row will store before the second and next one. You can also compute the address of an element in row-major order by the following formula,

Address(A[I][J]) = Base_Address + w(N(I - 1) + (J - 1))

where w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.

Column-major order

The elements in column-major order are stored column by column in memory. Elements of the first column will store before the second and next one. You can also compute the address of an element in column-major order by the following formula,

Address(A[I][J]) = Base_Address + w(M(J - 1) + (I - 1))

where w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.

 

 

 

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor