C Programming Lesson - Multidimensional array (3D Array)
By rajkishor09
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.
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();
}
Problem solving with C
|
|
Programming Video Games for the Evil Genius by Valdean C. Lembke, Thomas E....
Current Bid: $7.50
|
|
|
Stephens' C# Programming with Visual Studio 2010 24-Hou
Current Bid: $25.78
|
| No Photo |
Applications Programming in C++ by Martin Kalin, Ric...
Current Bid: $.95
|
|
|
C Programming Language (2nd Edition) by Brian W. Kerni
Current Bid: $27.98
|
|
|
5 Apple Mac iPhone Programming Books, Objective-C, Hillegass, Cocoa, iOS, etc...
Current Bid: $31.00
|
|
|
C Programming Language (2nd Edition) (Prentice Hall Software)
Current Bid: $27.48
|
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?
See results without votingUseful Articles
- C Programming Lesson - Call by Value and Call by Reference
Call by value and call by reference is the most confusing concept among new C language programmer. I also struggled with this, the reason may be my teacher is not explaining it in simple words or I was dumb. Whatever the reason is; here I will try to - How to swap two numbers without using third (temp) variable
This article explains how we can swap variable values without using third variable. There are different ways to do so and I tried including all know methods. You can get code in C, Java and C# language. - How to Setup Laptop Mobile Internet using Bluetooth
In this tutorial I am not going to say same thing which I already mentioned in How to Setup Laptop Mobile Internet using Mobile Phone tutorial. In this workshop you will need a Bluetooth dongle and... - How to enable Show Hidden Files and Folders option disabled after virus attack
If you ever faced such problem when your computer had a virus which ultimately disabled show hidden file and folders option, then you dont need to format or restore your setting. I have a trick... - Best Free PHP Tools & Editors for Every PHP Programmer
PHP (recursive acronym for: PHP Hypertext Preprocessor), is a widely-used, Open Source embedded scripting language. PHP is a server-side scripting language usually written in an HTML context. Unlike an ... - Turn Your PC Keyboard to Musical Keyboard using C Program
To accomplish this task you dont need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you...
Comments
thx for your clear explanation,
Thanks for the concept of 3D array
Nice example of the 3D arry but more example and easy to understand that type of example
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
how to imlpementthat array
how to store array of different data types?
@Vijay : I think you can try array of structure, please check this link.
thanx.I have solved my problem
nice,thank you for explain.
Are multidimensional arrays can be returned in a function? if can,i need an example, thx be4.
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.
Thank u for the clear explanation.
Thanks it's really nice........
in 3-D array 1st one is page 2nd is row and 3rd is coloum.is it right??
@Santu: that is right...
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
The example would be more useful if all of the dimensions were different [2][3][4] rather than [3][3][3].
its very useful for me to refer my doubt
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
its vry useful to us vry nice................
sir am live in punjab,and i have solve my problam.thanks sir.





Parvez 19 months ago
Thank you. Nicely explained.