Multi dimensional Array in C

Preview
You must first complete Two dimensional array in C | Initialization of Two dimensional array in C before viewing this Lesson

Multi dimensional Array in C

Array having more than two dimensions is known as multi-dimensional array. The limit of dimensions in a multidimensional array is specified by the compiler. It may depend upon the size of main memory available in computer.  We can use more than two subscripts to specify the size of a multidimensional array or to refer to any element of the multidimensional array.  The syntax for declaring a mutlidimensional array is:

<Data_Type> Arr_name[s1][s2][s3]……………….;

Here, Data_Type refers to any valid data type of C language. It can be any primary data type like int, float, char etc or can be any secondary data type like structure and union.

Arr_name  specifies the name of array as defined by the programmer. The name of array must follow all the rules of a valid identifier of C language.

[ ] Square brackets are known as subscript, they are used to declare size of array within them.

s1,s2,s3,…. specify the dimensions in the multidimensional array. They should be integer values.


Program to demonstrate the use of a multi dimensional array.
#include<stdio.h>
int main()
{
int a[2][2][2]={3,4,5,6,6,3,4,5};
printf(“\na[0][0][0]=%d”,a[0][0][0]);
printf(“\na[0][0][1]=%d”,a[0][0][1]);
printf(“\na[0][0][0]=%d”,a[0][1][0]);
printf(“\na[0][0][0]=%d”,a[0][1][1]);printf(“\na[1][0][0]=%d”,a[1][0][0]);
printf(“\na[1][0][1]=%d”,a[1][0][1]);
printf(“\na[1][0][0]=%d”,a[1][1][0]);
printf(“\na[1][0][0]=%d”,a[1][1][1]);
return(0);
}
Output
a[0][0][0]=3
a[0][0][1]=4
a[0][0][0]=5
a[0][0][0]=6
a[1][0][0]=6
a[1][0][1]=3
a[1][0][0]=4
a[1][0][0]=5

Best Books of C





Lesson tags: multidimensional array in c, program of multi dimensional array in c
Back to: C Programming Language