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

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.

Codzify's

**1:1 & 1:Many Mentorship Program**

Be job-ready. Learn how to crack the interviews with industry ready training program.

Learn More

## Data Structures & Algorithms

## What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

## 3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

## Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

## Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

## Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

## Javascript

## Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

## Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

## Bootstrap

## Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

## How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

## Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

## Bootstrap Buttons

Let's discuss about the button classes in bootstrap.