Multi-Dimensional Arrays (3D Arrays) in C Programming Language - Owlcation - Education
Updated date:

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

Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.

how-to-work-with-multidimensional-array-in-c-programming

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:

Declaration and Initialization 3D Array

#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:

how-to-work-with-multidimensional-array-in-c-programming

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

© 2009 RAJKISHOR SAHU

Comments

Slavek on December 16, 2019:

Hi, thanks for the article, but it unfortunately don't help anyone to handle 3D-arrays with functions.

I mean, that any function call can copy or change variable data in its storage, but when you call func wth "char" type parameter, you always type it like — void func_name(char * var_name) — so, if you have to change the value, you type it in like that, and when have not to — you only add "const" before "char * var"...

Could u please explain how to behave properly when I have to operate 3D-arrays in my functions? How I have to write function prototype to change the value of 3D-array?

void func_name(char *array[][][]) — is that correct?

void func_name( char *(***array) ) — or maybe like that?

if I have to operate with elements in EACH dimension, do I have to prototype functions like this? :

void func_name( char *(*array), char *(**array), char *(***array) ) {. . .}

Anyway, thank you for your site!

Neha Sharma on October 05, 2019:

Features are not available

p0st1e64 on July 15, 2019:

Thank you, a clear explanation and the 3D array conceptional view made it very clear to me

Muhammad Muddassar from Pakistan on June 22, 2019:

Thanks! For the complete information

Good on June 08, 2019:

Good one

manish on February 19, 2019:

its was helpful for me

Nitin Gupta on January 22, 2019:

How 3-dimensional array store in column major order?

la vita e bella on December 27, 2018:

very helpful for an inexperienced programmer

Jeevan on November 22, 2018:

Nice

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on September 19, 2018:

Thank You guys, let me know if you have any doubts.

chinni on September 19, 2018:

super, very understandable

Dhruv Gaharwar on August 30, 2018:

Sorry but I can not understand this logic

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on August 07, 2018:

thanks mavesh

Mahvesh on August 07, 2018:

Its really understandable:-)

Meenakshi on April 25, 2018:

I need to store the value to the 3 dimensions from 2 dimensions either in c or java...can any tell me plz

Roma Lohar on March 05, 2018:

Thanks alot.. this is really helpful

Mayur on February 03, 2018:

Nice help

Shivam Sharma on January 17, 2018:

can u tell me the logic to make star pattern in c

Asad on December 13, 2017:

yes it help me

Habib ahmad on December 11, 2017:

thank you so much..

Bappa saikh on November 13, 2017:

Thank you brother

Mike on October 16, 2017:

An example where the values of the array size values are different will help distinguish how the size will be affected

kushi on October 12, 2017:

looking for more appropriate example

Maheswari on September 20, 2017:

This tutorial is very useful.but please explain how to implement in 3d arithmetic operation in this array please explain............

Khushi on September 18, 2017:

Nice but difficult

Ankitha on August 20, 2017:

please tell me how to represent character 3dimensional matrix

Riya on June 09, 2017:

Thank you.This is very useful for us.

priya on February 18, 2017:

thank u but very difficult

Udit Tiwari on December 05, 2016:

Could you please tell me

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

taurus on November 11, 2016:

Bang on, thank you

K.Akila Jetkin on October 10, 2016:

Nice

wycliff on August 31, 2016:

so helpful. satisfied

Tarun on August 23, 2016:

explain the multi dimensional array using 3d array multiplication

priti on May 20, 2016:

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

Karan on April 15, 2016:

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;

}

Parveen Anand on March 19, 2016:

Great explain the 3D programmin g. Thanks alot.

balaganesh on February 23, 2016:

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

jp on February 12, 2016:

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

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

how about in character or string?

how to initialize in 3d array?

ganesh on September 14, 2014:

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?????

SANDEEP VERMA on February 24, 2014:

THANK YOU........YOUR PROGRAMME IS GOOD

ritesh khanna on October 17, 2013:

pls help

natasha jane on October 17, 2013:

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,,

ritesh patel on September 26, 2013:

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

ivy biswas on August 13, 2013:

i need to know the syntax for 3 D Array

Chris Goodwin on March 27, 2013:

has expanded my view

alfred on December 01, 2012:

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

sukh on March 15, 2012:

sir am live in punjab,and i have solve my problam.thanks sir.

tharani on February 27, 2012:

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

Jamal on February 18, 2012:

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

thivya on February 02, 2012:

its very useful for me to refer my doubt

Janet on January 29, 2012:

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

Ali on January 12, 2012:

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

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on January 05, 2012:

@Santu: that is right...

santu das on January 05, 2012:

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

Razi Ahmad on December 20, 2011:

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

R.Nandhini on December 08, 2011:

Thank u for the clear explanation.

c language easy for concepts on December 06, 2011:

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.

drosanda on December 05, 2011:

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

ian on December 02, 2011:

nice,thank you for explain.

Rintu_achiever on October 18, 2011:

thanx.I have solved my problem

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on October 13, 2011:

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

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

Vijay on October 12, 2011:

how to store array of different data types?

santhosh on October 12, 2011:

how to imlpementthat array

Ashish Kasampara on August 07, 2011:

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

Suraj Verma on January 30, 2011:

Thanks for the concept of 3D array

chokies on October 31, 2010:

thx for your clear explanation,

Parvez on October 21, 2010:

Thank you. Nicely explained.