Table of Contents
Multidimensional array in C
We can have an array as an element of another array, in simple terms array of an array and this is called a multidimensional array.
In this type of array each element of the array is an array itself.
Two dimensional arrays are the most common simplest form of multidimensional arrays.
We can have more dimensions but it will get complicated and hard to handle.
All the elements in the inner array are of the same data type.
Syntax :
Data_type Array_name[Size_of_array_1][Size_of_array_2][Size_of_array_n];
If we are creating three dimensional array then we will create like this :
int numbs[4][3][7];
Now let’s talk about the most common multidimensional array i.e. two dimensional array.
In overview, a two dimensional array is an array where each element is one array.
E.g of two dimensional array
int a[2][4]={1,2,3,4,5,6,7,8};
In the above example first size i.e. 2 represents the size of outer array and second size i.e. 4 represents the size of inner array.
We can see it as a grid of 2×4 where there are 2 rows and 4 columns, and each block in the grid stores int value. (please refer to the image below)

Initialization of multidimensional array
int a[2][4] = {{1,2,3,4},{5,6,7,8}};
int a[][4] = {1,2,3,4,5,6,7,8};
In second we can see that we have not specified the size of the outer array but we did specify the size of the inner array.
Here the compiler will calculate a size required for the outer array based on the number of the inner arrays.
In this case we have set the size of the inner array to 4 and there are 8 elements in total so we will have 2 inner arrays or we can say that we have 2 array elements.
So the compiler will set the size of the outer element to 2, and if we have set the size of the inner arrays to 2 then we would have 4 inner arrays then the compiler would have set size of outer array to 4.
Access element from two dimensional array.
Let’s take above array as an example
int a[2][4] = {{1,2,3,4},{5,6,7,8}};
Now if we want to print or access value 7 then we have to specify its position or index.
Now first we must know the index of value ‘7’ in that particular inner array.
We can say that the index number of ‘7’ in the inner array is 2. Next we have to check the index number of that particular inner in outer array and that is 1.
So if we want to access or print value 7 from the above array then first we have to specify the index number of the inner array which has it and then index number of 7 in the inner array.
We will use the following line to print/access the value 7 from the above array.
printf(“%d”,a[1][2]);
In the above code, the first index i.e. 1 is the index of the inner array in the outer array and second index i.e. 2 is an index of ‘7’ in the inner array.
Example of two dimensional array in C
/*
Author : ITVoyagers (https://itvoyagers.in/)
Date :25th December 2018
Description : Two Dimensional Array.
*/
#include <stdio.h>
int main()
{
int a[][3]={1,2,3,4,5,6,7,8,9},row,col;
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
printf("%d t",a[row][col]);
}
printf("n");
}
return 0;
}
Advantages of multidimensional array
Two dimensional arrays are easy to access and maintain.
There is no need for various variables we can use one single array to store all value of same data types
It is faster to access.
In his memory locations are allotted in contiguous form.
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
