AcademiaAgriculture & FarmingHumanitiesSocial SciencesSTEM

Multi-Dimensional Arrays (3D Arrays) in C Programming Language

Updated on September 30, 2014
Source

C allows for arrays of two or more dimensions. A two-dimensional (2D) array is an array of arrays. A three-dimensional (3D) array is an array of arrays of arrays.

In C programming an array can have two, three, or even ten or more dimensions. The maximum dimensions a C program can have depends on which compiler is being used.

More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays.

How to Declare a Multidimensional Array in C

A multidimensional array is declared using the following syntax:

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

Where each d is a dimension, and dn is the size of final dimension.

Examples:

  1. int table[5][5][20];
  2. float arr[5][6][5][6][5];

In Example 1:

  • int designates the array type integer.
  • table is the name of our 3D array.
  • Our array can hold 500 integer-type elements. This number is reached by multiplying the value of each dimension. In this case: 5x5x20=500.

In Example 2:

  • Array arr is a five-dimensional array.
  • It can hold 4500 floating-point elements (5x6x5x6x5=4500).

Can you see the power of declaring an array over variables? When it comes to holding multiple values in C programming, we would need to declare several variables. But a single array can hold thousands of values.

Note: For the sake of simplicity, this tutorial discusses 3D arrays only. Once you grab the logic of how the 3D array works then you can handle 4D arrays and larger.

Explanation of a 3D Array

Let's take a closer look at a 3D array. A 3D array is essentially an array of arrays of arrays: it's an array or collection of 2D arrays, and a 2D array is an array of 1D array.

It may sound a bit confusing, but don't worry. As you practice working with multidimensional arrays, you start to grasp the logic.

The diagram below may help you understand:

3D Array Conceptual View
3D Array Conceptual View
3D array memory map.
3D array memory map.

Initializing a 3D Array in C

Like any other variable or array, a 3D array can be initialized at the time of compilation. By default, in C, an uninitialized 3D array contains “garbage” values, not valid for the intended use.

Let’s see a complete example on how to initialize a 3D array:

Syntax:

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, k;
int arr[3][3][3]=   
		{
		    {
			{11, 12, 13},
			{14, 15, 16},
			{17, 18, 19}
		    },
		    {
			{21, 22, 23},
			{24, 25, 26},
			{27, 28, 29}
			},
			{
			{31, 32, 33},
			{34, 35, 36},
			{37, 38, 39}
			},
		};
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
		for(k=0;k<3;k++)
		{
		printf("%d\t",arr[i][j][k]);
		}
		printf("\n");
	}
	printf("\n");
}
getch();
}

Print:

In the code above we have declared a multidimensional integer array named “arr” which can hold 3x3x3 (or 27) elements.

We have also initialized the multidimensional array with some integer values.


As I said earlier, a 3D array is an array of 2D arrays. I have divided elements accordingly for easy understanding. Looking at the C code sample above,

  • In lines 9-13, 14-18, and 19-23, each block is a 2D array.
  • Collectively, lines 2-24 make a 3D array.

To call values from the array, imagine the 3D array above as a collection of tables. Each nested bracket cluster is a table with rows and columns. To access or store any element in a 3D array you need to know its table number, row number, and column number.

An example: You need to access value 25 from the above 3D array. So, first check the table: in this case, 25 is in table 1 (remember: tables, rows, columns are counted starting at 0, so the second table is table 1). Once you find the table number now check which row of that table has the value and then check the column number. So applying above logic, 25 located in table 1, row 1, and column 1, hence the address is arr[1][1][1]. Print this address and you will get the output: 25.

The Conceptual Syntax of a 3D Array in C

The conceptual syntax for 3D array is this:

data_type array_name[table][row][column];

If you want to store values in any 3D array point first to table number, then row number, and lastly to column number.

Some hypothetical examples:

arr[0][1][2] = 32;
arr[1][0][1] = 49;

Storing Values in a Continuous Location Using a Loop

The pointer syntax above assigns values to a particular location of an array, but if you want to store values in multiple locations automataically then you should use a loop.

Here is an example using the for loop command:

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, k, x=1;
int arr[3][3][3];
clrscr();
printf(":::3D Array Elements:::\n\n");

for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
		for(k=0;k<3;k++)
		{
		arr[i][j][k] = x;
		printf("%d\t",arr[i][j][k]);
		x++;
		}
		printf("\n");
	}
	printf("\n");
}
getch();
}

Your opinion

Is this tutorial helpful for you?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Parvez 6 years ago

      Thank you. Nicely explained.

    • profile image

      chokies 6 years ago

      thx for your clear explanation,

    • profile image

      Suraj Verma 6 years ago

      Thanks for the concept of 3D array

    • profile image

      Ashish Kasampara 5 years ago

      Nice example of the 3D arry but more example and easy to understand that type of example

    • profile image

      santhosh 5 years ago

      how to imlpementthat array

    • profile image

      Vijay 5 years ago

      how to store array of different data types?

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      @Vijay : I think you can try array of structure, please check this link.

      https://hubpages.com/technology/Arrays-of-Structur...

    • profile image

      Rintu_achiever 5 years ago

      thanx.I have solved my problem

    • profile image

      ian 5 years ago

      nice,thank you for explain.

    • profile image

      drosanda 5 years ago

      Are multidimensional arrays can be returned in a function? if can,i need an example, thx be4.

    • profile image

      c language easy for concepts 5 years ago

      dear

      sir,

      thanks you sir c language understand help and concpet for easy understand.plz sir requst for c langague method

      for easy understand.plz sir send my e-mail

      id sujata.waghmare798@gmail.com.

      i am mca student and the not undertstand the c and c is very important of IT student plz sir help send replay my email id.

      thanks sir.

    • profile image

      R.Nandhini 5 years ago

      Thank u for the clear explanation.

    • profile image

      Razi Ahmad 5 years ago

      Thanks it's really nice........

    • profile image

      santu das 5 years ago

      in 3-D array 1st one is page 2nd is row and 3rd is coloum.is it right??

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      @Santu: that is right...

    • profile image

      Ali 5 years ago

      hi

      i need to write a code that emulates an LED display, and for the numbers ive set a 3D array. the problem i have is getting it to read from an input. ive tried storing the input in a char array and reading it using scanf() but it doesn't work. can you please tell me what i need to do

      thanks

    • profile image

      Janet 5 years ago

      The example would be more useful if all of the dimensions were different [2][3][4] rather than [3][3][3].

    • profile image

      Jamal 5 years ago

      what is the purpose of multiple dimensional arrays if i can do the same with a one intentional array

      like what ares the advantages and stuff like that

    • profile image

      tharani 5 years ago

      its vry useful to us vry nice................

    • profile image

      alfred 4 years ago

      write a program that reads in an array type int. you may assume that there are fewer than 50 entries in that array. your program determines how many entries are used. the output is to be a two column is a list of the district array elements; the second column is the count of the number of occurences of each element the list should be sorted on entries int first column largest to smallest

    • profile image

      Chris Goodwin 4 years ago

      has expanded my view

    • profile image

      ivy biswas 3 years ago

      i need to know the syntax for 3 D Array

    • profile image

      ritesh patel 3 years ago

      welll good, but i would like to know that how to perform subtraction of two array of 3d. for example a[0]-a1[1]. could u help me

    • profile image

      natasha jane 3 years ago

      hey........the codes and the explanation really helped me..bt I really cant understand the difference between the 3D array being demonstrated here

      and a 2D array with multiple endls ???pls help me to figure out the difference

      thankyou,,

    • profile image

      ritesh khanna 3 years ago

      pls help

    • profile image

      ganesh 2 years ago

      in three dimentional array arr[3][3][3] means 3table 3rows and 3columns like that in four dimentional array arr[3][3][3][3] means?????

    • profile image

      jp 15 months ago

      arr[0][1][2] = 32;

      arr[1][0][1] = 49;

      how about in character or string?

      how to initialize in 3d array?

    • profile image

      balaganesh 15 months ago

      i can't understand that 3D array. can u explain this

    • profile image

      Parveen Anand 14 months ago

      Great explain the 3D programmin g. Thanks alot.

    • profile image

      Karan 13 months ago

      Can you please explain how below array will be represented {Similar to pictorial representation shown above (Fig. 3D array memory map)}

      I believe that in the 0th 2-D array there should be 3 rows (row0 , row1,row2)with 2 elements

      But with some examples I've found it to be 2 rows with 3 elements in oth 2-D array and similar in 1st 2-d array

      Below is the example

      int main()

      {

      int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,3},{2,3},{3,4}}};

      printf("%d\n",sizeof(int));

      printf("%u %u %u %d\n",a,*a,**a,***a);

      printf("%u %u %u %d\n",a+1,*a+1,**a+1,***a+1);

      getch();

      return 0;

      }

    • profile image

      priti 12 months ago

      how to find the address of an item placed at particular location ?

    • profile image

      Tarun 9 months ago

      can u explain with the example of multi dimensional arrays???

    • profile image

      Tarun 9 months ago

      explain the multi dimensional array using 3d array multiplication

    • profile image

      wycliff 8 months ago

      so helpful. satisfied

    • profile image

      K.Akila Jetkin 7 months ago

      Nice

    • profile image

      Udit Tiwari 5 months ago

      Could you please tell me

      A(1:8,-5:5,-10:5) is an 3D array then what will be the size of A?

    • profile image

      priya 3 months ago

      thank u but very difficult

    Click to Rate This Article