Flag This Hub

C Programming Lesson - Multidimensional array (3D Array)

By


C Programming Array
C Programming Array

C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array having two dimensions called 2D array and so on. So in C programming an array can have two or three or four or even ten or more dimensions. More dimensions in an array means more data it can hold and of course more difficulties to manage and understand these arrays. A multidimensional array has following syntax:

Syntax:

type array_name[d1][d2][d3][d4]………[dn];
Where dn is the size of last dimension.

Example:

int table[5][5][20];
float arr[5][6][5][6][5];

In our example array “table” is a 3D (A 3D array is an array of arrays of arrays.) array which can hold 500 integer type elements. And array “arr” is a 5D array which can hold 4500 floating-point elements. Can see the power of array over variable? When it comes to hold multiple values in a C programming, we need to declare several variables (for example to store 150 integers) but in case of array, a single array can hold thousands of values (depending on compiler, array type etc).

Note: To make this multidimensional array example simple I will discuss 3D array for the sake of simplicity. Once you grab the logic how 3D array works then you can handle 4D array or any multidimensional array easily.

How to Declaration and Initialization 3D Array

Before we move to serious programming let's have a look of 3D array. A 3D array can be assumed as an array of arrays of arrays, it is array (collection) of 2D arrays and as you know 2D array itself is array of 1D array. It sounds a bit confusing but don't worry as you will lead your learning on multidimensional array, you will grasp all logic and concept. A diagram can help you to understand this.

3D Array Conceptual View
See all 2 photos
3D Array Conceptual View
3D array memory map.
3D array memory map.

We can initialize a 3D array at the compile time as we initialize any other variable or array, by default an un-initialized 3D array contains garbage value. Let’s see a complete example on how we can work with a 3D array.

Example of 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();
}

C Books

C Programming Language (2nd Edition)
Amazon Price: $35.64
List Price: $67.00
Programming in C (3rd Edition)
Amazon Price: $24.00
List Price: $49.99
Absolute Beginner's Guide to C (2nd Edition)
Amazon Price: $19.00
List Price: $39.99
C Programming: A Modern Approach, 2nd Edition
Amazon Price: $66.27
C Primer Plus (5th Edition)
Amazon Price: $30.63
List Price: $54.99

So in the above example we have declared multidimensional array and named this integer array as “arr” which can hold 3x3x3 (27 integers) elements. We have also initialized multidimensional array with some integer values.
As I told you earlier that a 3D array is array of 2D array therefore I have divided elements accordingly so that you can get 3D array better and understand it easily. See the C code sample above, line no. 9-13, 14-18 and 19-23, each block is a 2D array and collectively from line no. 2-24 makes a 3D array. You can also assign values to this multidimensional array in other way like this.

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};

This kind of C multidimensional array (3D array) declaration is quite confusing for new C programmers; you cannot guess location of array element by just looking at the declaration. But look at the above multidimensional array example where you can get a clear idea about each element location. For example, consider 3D array as a collection of tables, to access or store any element in a 3D array you need to know first table number then row number and lastly column number. For instance you need to access value 25 from above 3D array. So, first check the table (among 3 tables which table has the value), once you find the table number now check which row of that table has the value again if you get the row no then check column number and you will get the value. So applying above logic, 25 located in table no. 1 row no. 1 and column no. 1, hence the address is arr[1][1][1]. Print this address and you will get the output.

So the conceptual syntax for 3D array stands like this.

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

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

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

Above code is for assigning values at particular location of an array but if you want to store value in continuous location of array then you should use loop. Here is an example using for loop.

#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();
}

I hope this will make concept of multidimensional array a bit easy for you. Feel free to ask your question and doubts.

Your opinion

Is this tutorial helpful for you?

  • Yes, very much.
  • No, Not at all
  • I know this, but looking for more examples.
See results without voting

Comments

Parvez 19 months ago

Thank you. Nicely explained.

chokies 19 months ago

thx for your clear explanation,

Suraj Verma 16 months ago

Thanks for the concept of 3D array

Ashish Kasampara 9 months ago

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

Mo Ne 8 months ago

So , can I ask you this because i want to store the values inside multi array to single array for exporting to c# application? I am wondering this way of writing is also correct.Thanks

int sgarray[27];

int count=0;

for(i=0;i

santhosh 7 months ago

how to imlpementthat array

Vijay 7 months ago

how to store array of different data types?

rajkishor09 7 months ago

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

http://rajkishor09.hubpages.com/_eknow/hub/Arrays-

Rintu_achiever 7 months ago

thanx.I have solved my problem

ian 5 months ago

nice,thank you for explain.

drosanda 5 months ago

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

c language easy for concepts 5 months 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.

R.Nandhini 5 months ago

Thank u for the clear explanation.

Razi Ahmad 5 months ago

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

santu das 4 months ago

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

rajkishor09 4 months ago

@Santu: that is right...

Ali 4 months 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 doesnt work. can you please tell me what i need to do

thanks

Janet 3 months ago

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

thivya 3 months ago

its very useful for me to refer my doubt

Jamal 3 months 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

tharani 2 months ago

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

sukh 2 months ago

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

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working