Multi Dimensional Array In C Programming

Multi-Dimensional array in C programming; Through this tutorial, i am going to show you how to make, initialize and use multi dimensional arrays in c programming language.

Multi Dimensional Array In C Programming

Let’s see step by step on how to make, initialize and use multi dimensional arrays in c programming language:

  • Definition of Multi Dimensional Array in C
  • Declaration of Multi Dimensional Array in C
  • Initialization of Multi Dimensional Array in C
  • Example 1 – Multi Dimensional Array in C

Definition of Multi Dimensional Array in C

A three-dimensional (3D) or Multi dimensional array is an array of arrays of arrays.

Let’s understand by example; An array can contain two, three, or even ten or more dimensions.

Declaration Multi Dimensional Array in C

Use the below given syntax to declare a multi-dimensional array in the c programming language; as shown below:

type array_name[d1][d2][d3][d4]………[dn];

Let’s see the following example for how to declare multi-dimensional array in c programming; as shown below:

int table[5][5][20];

Initialization of Multi Dimensional Array

Use the below given example for initializing a three-dimensional or multi dimensional array in a similar way to a two-dimensional array; as shown below:

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Example 1 – Multi Dimensional Array in C

Let’s take an first example of multi dimensional array in c; is as follow:

// C Program to store and print 12 values entered by the user
#include <stdio.h>
int main()
{
  int test[2][3][2];
  printf("Enter 12 values: \n");
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        scanf("%d", &test[i][j][k]);
      }
    }
  }
  // Printing values with proper index.
  printf("\nDisplaying values:\n");
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
      }
    }
  }
  return 0;
}

Output:

Enter 12 values: 
1
2
3
4
5
6
7
8
9
10
11
12

Displaying Values:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

Be First to Comment

Leave a Reply

Your email address will not be published.