What is a multi-dimensional array in C language?


An array is a group of related items that store with a common name.

Syntax

The syntax for declaring an array is as follows −

datatype array_name [size];

Types of arrays

Arrays are broadly classified into three types. They are as follows −

  • One – dimensional arrays
  • Two – dimensional arrays
  • Multi – dimensional arrays

Initialization

An array can be initialized in two ways. They are mentioned below −

  • Compile time initialization.
  • Runtime initialization.

Multi-dimensional array

  • ‘C’ allows arrays of 3 (or) more dimensions.
  • The exact limit is determined by compiler.

Syntax

The syntax is as follows −

datatype arrayname [size1] [size2] ----- [sizen];

For example,

  • For 3 – dimensional array −
    • int a[3] [3] [3];

No. of elements = 3*3*3 = 27 elements

Program

Following is the C program for multi-dimensional array −

 Live Demo

#include<stdio.h>
main ( ){
   int a[2][2] [2] = {1,2,3,4,5,6,7,8};
   int i,j,k;
   printf ("elements of the array are");
   for ( i=0; i<2; i++){
      for (j=0;j<2; j++){
         for (k=0;k<2; k++){
            printf("%d", a[i] [j] [k]);
         }
      }
   }
}

Output

The output is stated below −

Elements of the array are :
1 2 3 4 5 6 7 8

Consider another program for Multi-Dimensional Array in C programming language, as given below −

Example

 Live Demo

#include<stdio.h>
int main(){
   int tab, row, col;
   int students[3][3][2] = { { {1, 2}, {4, 5}},{ {2, 4}, {3, 5} },{ {7,8},{9,3}}};
   for (tab = 0; tab < 3; tab++){
      for (row = 0; row < 3; row++){
         for (col =0; col < 2; col++){
            printf("students[%d][%d][%d]= %d
", tab, row, col,             students[tab][row][col]);          }       }    }    return 0; }

Output

The output is stated below −

students[0][0][0]= 1
students[0][0][1]= 2
students[0][1][0]= 4
students[0][1][1]= 5
students[0][2][0]= 0
students[0][2][1]= 0
students[1][0][0]= 2
students[1][0][1]= 4
students[1][1][0]= 3
students[1][1][1]= 5
students[1][2][0]= 0
students[1][2][1]= 0
students[2][0][0]= 7
students[2][0][1]= 8
students[2][1][0]= 9
students[2][1][1]= 3
students[2][2][0]= 0
students[2][2][1]= 0

Updated on: 15-Mar-2021

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements