Multi Dimensional Array in C

Till now we have seen all about one dimensional array, where we were storing data in liner direction, now we will see multi-dimensional array in c programming. C supports concept of multi-dimensional array like Two-dimensional array, Three-dimensional array, similarly n-dimensional array. Here Two-dimensional arrays are widely used in programming to solve very complex problems. Three-D array, Four-D array, n-D array are rarely used in programming. In this tutorial we will learn Two-dimensional array in detail and we will also understand the concept of Three-D, Three-D, …., n-D arrays.

Multi Dimensional Array in C

Two - Dimensional Array

Two-Dimensional array is a fixed length, homogeneous data type, row and column based data structure which is used to store similar data type element in row and column based structure. See below image.

2 - Dimensional Array in C

A two dimensional array is referred as a matrix or a table. A matrix has two subscripts, one denotes the row and another denotes the column.

  • The first dimension might refer to the row number.
  • Second dimension refer to the column number.

Two-Dimensional Array Declaration in C


data_type array_name [rows] [column];


int myNumber [3] [4];

Note: Number of elements in array = rows * columns

Two-Dimensional Array Initialization


data_type array_name [m] [n] = {a1, a2, a3, ...., amn} ;


m → number or row

n → number or column

a1, a2, a3, ...., amn are array elements.

Two-Dimensional array or multidimensional array can be initialised by two ways, which are listed below.

  1. By Hard – Coded values
  2. By User Input

By Hard –Coded values

In this method Two-Dimensional array can be initialized by 2 types i.e. either we can pass all element together or we can we can pass value by grouping rows values.

A. All Elements Together

int c_students [2] [5] = {1,2,3,4,5,12,17,11,23,9} ;

In above example array c_students is a two dimensional array which have 2 rows and 5 column, so the array can hold 2*5 = 10 values of similar types which are passed within { and } separated by comma.

B. By Grouping rows values

Here we are grouping the value of row together within {} separated by comma, and these row are also separated with comma.

int c_students [2] [5] = { {1,2,3,4,5}, {12,17,11,23,9} } ;

The array c_students have 2 rows and 5 columns, so we have grouped row value together means row 0 values in one {} separated by comma and row 1 values in other {} separated by comma and these two set of {} are also separated by comma within main {}. Let's take another example.

int test [6] [2] = { {23,45}, {32,5}, {7,43}, {74,97}, {734,75}, {85,80} } ;

In above example we have 6 rows and 2 column, we have grouped individual row values together, so there is 6 groups for 6 rows.

int test [6] [ ] = { {23,45}, {32,5}, {7,43}, {74,97}, {734,75}, {85,80} } ;

If we do not define row or column value then it assigned by compiler automatically according to initialized value, like above example.

Three-Dimensional Array Initialization

Three dimensional array are also initialized in same way as Two-Dimensional array, Let’s understand Three-Dimensional Array initialization by taking an example.

int myDia [2] [3] [4] = {
  { {23,45,47,74}, {32,5,75,85}, {7,4,75,85}},
  { {33,49,47,45}, {4,5,95,74}, {24,43,44,23}}
} ;

Accessing Two-Dimensional Array Elements

An array elements are accessed based on their index, in one dimensional array we pass one index, in Two-Dimensional array we pass two value of index, in Three-Dimensional array we pass three value of index and similarly for n-dimensional array we pass n value of index.

int a;
In One-Dimensional Array
a = array_name[index_vlue1];
In Two-Dimensional Array
a = array_name [index_vlue1] [index_vlue2];
In Three-Dimensional Array
a = array_name [index_vlue1] [index_vlue2] [index_vlue3];


int c_students [2] [5] = {1,2,3,4,5,12,17,11,23,9} ;


c_students [0] [0] == 1 c_students [0] [1] == 2 c_students [0] [2] == 3 c_students [0] [3] == 4 c_students [0] [4] == 5 c_students [1] [0] == 12 c_students [1] [1] == 17 c_students [1] [2] == 11 c_students [1] [3] == 23 c_students [1] [4] == 9

Two-Dimensional Array Initialization by User Input

Let’s write a program to initialize a two dimensional array input value inserted by user


  • In above example we have declared two variables I and j, where I denotes row index and j denotes column index.
  • There are two for loops, outer for loop is used to count row number and inner for loop is used to count column number.
  • Outer loop will execute two times for condition i=0;i<2;i++
  • For one outer loop round, inner loop will execute 3 times for condition j=0;j<3;j++ , So in this way inner loop will execute total 6 times.

1. Step i=0

For i=0, inner loop executes 3 times and value will be insewrted in array at index

test[i][j] means test[0][0] index.
test[i][j] means test[0][1] index.
test[i][j] means test[0][2] index.

2. Step i=1

For i=1, inner loop executes 3 times and value will be insewrted in array at index

test[i][j] means test[1][0] index.
test[i][j] means test[1][1] index.
test[i][j] means test[1][2] index.

Two-Dimensional Array Examples

1. Example

Write a program to prints element of a two dimensional array, the array elements are predefined.



2. Example

Write a c program in which a two dimensional char array of size [3][4] accept input from user and print all input element.

Output Screen

Enter 12 characters
Array elements are
m c x s
z d e l
d w a j

3. Example

Write a program to print addition of two predefined matrix and it should also print predefined matrix elements.

Output Screen

First Matrix is
0 3 7
3 9 3
2 1 0
Second Matrix is
3 6 2
6 6 1
8 9 2
Addition of matrix 1 and matrix 2
3  9  9
9  15 4
10 10 2

See more programs on Two-Dimensional array in array programs section, now let’s learn how to use array with function, just click on next.

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at ""

© 2021 GDATAMART.COM (All Rights Reserved)