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

Updated on July 9, 2018
rajkishor09 profile image

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.

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:

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

© 2009 RAJKISHOR SAHU

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Jeevan 

      3 weeks ago

      Nice

    • rajkishor09 profile imageAUTHOR

      RAJKISHOR SAHU 

      2 months ago from Bangalore, Karnataka, INDIA

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

    • profile image

      chinni 

      3 months ago

      super, very understandable

    • profile image

      Dhruv Gaharwar 

      3 months ago

      Sorry but I can not understand this logic

    • rajkishor09 profile imageAUTHOR

      RAJKISHOR SAHU 

      4 months ago from Bangalore, Karnataka, INDIA

      thanks mavesh

    • profile image

      Mahvesh 

      4 months ago

      Its really understandable:-)

    • profile image

      Meenakshi 

      7 months ago

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

    • profile image

      Roma Lohar 

      9 months ago

      Thanks alot.. this is really helpful

    • profile image

      Mayur 

      10 months ago

      Nice help

    • profile image

      Shivam Sharma 

      11 months ago

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

    • profile image

      Asad 

      12 months ago

      yes it help me

    • profile image

      Habib ahmad 

      12 months ago

      thank you so much..

    • profile image

      Bappa saikh 

      13 months ago

      Thank you brother

    • profile image

      Mike 

      14 months ago

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

    • profile image

      kushi 

      14 months ago

      looking for more appropriate example

    • profile image

      Maheswari 

      15 months ago

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

    • profile image

      Khushi 

      15 months ago

      Nice but difficult

    • profile image

      Ankitha 

      16 months ago

      please tell me how to represent character 3dimensional matrix

    • profile image

      Riya 

      18 months ago

      Thank you.This is very useful for us.

    • profile image

      priya 

      22 months ago

      thank u but very difficult

    • profile image

      Udit Tiwari 

      2 years 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

      taurus 

      2 years ago

      Bang on, thank you

    • profile image

      K.Akila Jetkin 

      2 years ago

      Nice

    • profile image

      wycliff 

      2 years ago

      so helpful. satisfied

    • profile image

      Tarun 

      2 years ago

      explain the multi dimensional array using 3d array multiplication

    • profile image

      priti 

      2 years ago

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

    • profile image

      Karan 

      2 years 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

      Parveen Anand 

      2 years ago

      Great explain the 3D programmin g. Thanks alot.

    • profile image

      balaganesh 

      2 years ago

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

    • profile image

      jp 

      2 years 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

      ganesh 

      4 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

      SANDEEP VERMA 

      4 years ago

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

    • profile image

      ritesh khanna 

      5 years ago

      pls help

    • profile image

      natasha jane 

      5 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 patel 

      5 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

      ivy biswas 

      5 years ago

      i need to know the syntax for 3 D Array

    • profile image

      Chris Goodwin 

      5 years ago

      has expanded my view

    • profile image

      alfred 

      6 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

      sukh 

      6 years ago

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

    • profile image

      tharani 

      6 years ago

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

    • profile image

      Jamal 

      6 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

      thivya 

      6 years ago

      its very useful for me to refer my doubt

    • profile image

      Janet 

      6 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

      Ali 

      6 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

    • rajkishor09 profile imageAUTHOR

      RAJKISHOR SAHU 

      6 years ago from Bangalore, Karnataka, INDIA

      @Santu: that is right...

    • profile image

      santu das 

      6 years ago

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

    • profile image

      Razi Ahmad 

      6 years ago

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

    • profile image

      R.Nandhini 

      7 years ago

      Thank u for the clear explanation.

    • profile image

      c language easy for concepts 

      7 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

      drosanda 

      7 years ago

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

    • profile image

      ian 

      7 years ago

      nice,thank you for explain.

    • profile image

      Rintu_achiever 

      7 years ago

      thanx.I have solved my problem

    • rajkishor09 profile imageAUTHOR

      RAJKISHOR SAHU 

      7 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

      Vijay 

      7 years ago

      how to store array of different data types?

    • profile image

      santhosh 

      7 years ago

      how to imlpementthat array

    • profile image

      Ashish Kasampara 

      7 years ago

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

    • profile image

      Suraj Verma 

      7 years ago

      Thanks for the concept of 3D array

    • profile image

      chokies 

      8 years ago

      thx for your clear explanation,

    • profile image

      Parvez 

      8 years ago

      Thank you. Nicely explained.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, owlcation.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://owlcation.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)