An Array having more than one dimension is called Multi Dimensional array. In our previous article we discussed about Two Dimensional Array which is the simplest form of Multi Dimensional Array. In C Programming Language, by placing n number of brackets [ ] we can declare n-dimensional array where n is dimension number. For example,

int a[2][3][4] = Three Dimensional Array

int a[2][2][3][4] = Four Dimensional Array

In this article we will explain about the Three Dimensional Array in C for better understanding. You can try 4D array on your own.

## Syntax of a Multi Dimensional Array in C Programming

The basic syntax or, the declaration of multi dimensional array in C Programming is as shown below:

1 |
Data_Type Array_Name[Tables][Row_Size][Column_Size] |

**Data_type:**It will decide the type of elements it will accept. For example, If we want to store integer values then we declare the Data Type as int, If we want to store Float values then we declare the Data Type as float etc**Array_Name:**This is the name you want to give it to array.**Tables:**It will decide the number of tables an array can accept. Two Dimensional Array is always single table with rows and columns whereas Multi Dimensional array is more than 1 table with rows and columns.**Row_Size:**Number of Row elements an array can store. For example, Row_Size =10 then array will have 10 rows.**Column_Size:**Number of Column elements an array can store. For example, Column_Size = 8 then array will have 8 Columns.

We can calculate the maximum number of elements in a Three Dimensional using: [Tables] * [Row_Size] * [Column_Size]

**For Example**

int Employees[2][4][3];

- Here, we used int as the data type to declare an array. So, above array will accept only integers. If you try to add float values then it will through an error.
- Employees is the array name
- Number of tables = 2 so, this array will hold maximum 2 levels of data (rows and columns).
- The Row size of an Array is 4 it means, Employees array will only accept 4 integer values as rows.
- If we try to store more than 4 then it will throw an error.
- We can store less than 4. For Example, If we store 2 integer values then remaining 2 values will be assigned to default value (Which is 0).

- The Column size of an Array is 3 it means, Employees array will only accept 3 integer values as columns.
- If we try to store more than 3 then it will throw an error.
- We can store less than 3. For Example, If we store 1 integer values then remaining 2 values will be assigned to default value (Which is 0).

- Finally, Employees array can hold maximum of 24 integer values (2 * 4 * 3 = 24).

## Multi Dimensional Array Initialization

We can initialize the Multi Dimensional Array in multiple ways

#### First Approach

1 2 3 4 5 |
int Employees[2][4][3] = { { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }, { {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} } }; |

Here, We have 2 tables and 1^{st} table holds 4 Rows * 3 Columns and 2^{nd} table also holds 4 Rows * 3 Columns

First three elements of the first table will be 1^{st} row, second three elements will be 2^{nd} row, next 3 elements will be 3^{rd} row and last 3 element will be 4^{th} row. Here we divided them into 3 because our column size = 3 and We surrounded each row with curly braces ({}). It is always good practice top use the curly braces to separate the rows.

Same for the second table.

**Second Approach**

1 2 3 4 |
int Employees[2][ ][3] = { { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }, { {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} } }; |

Here, We did not mention the row size but the compiler is intelligent enough to calculate the size by checking the number of elements inside the row.

We can also write

1 2 3 4 5 |
int Employees[2][4][ ] = { { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }, { {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} } }; |

#### Third Approach

1 2 3 4 5 |
int Employees[2][4][3] = { { { 10 }, {15, 25}, {22, 44, 66}, {33, 55, 77} }, { {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} } }; |

Here we declared Employees array with row size = 4 and column size = 3 but we only assigned 1 column in the 1^{st} row and 2 columns in the 2^{nd} row of the first table. In these situations, the remaining values will be assigned to default values (0 in this case).

The above array will be:

1 2 3 |
int Employees[2][4][3] = { { {10, 0, 0}, {15, 25, 0}, {22, 44, 66}, {33, 55, 77} }, { {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} } }; |

#### Fourth Approach

Above 3 ways are good to store small number of elements into the array, What if we want to store 100 rows or 50 column values in 20 tables. It will be a nightmare to add all of them using any of the above mentioned approaches. To resolve this, we can use the loop concept here:

1 2 3 4 5 6 7 8 9 10 11 12 |
int tables, rows, columns, Employees[20][100][100]; for (tables = 0; tables < 20; tables ++) { for (rows = 0; rows < 100; rows++) { for (columns =0; columns < 100; columns++) { Employees[tables][rows][columns] = tables + rows + columns ; } } } |

## Accessing Multi Dimensional Array Elements

In C Programming, We can access the array elements using indexes. Using index we can access or alter/change each and every individual element present in the array separately. Index value starts at 0 and end at n-1 where n is the size of a row or column.

For example, if an Array_name[4][8][5] will stores 8 row elements and 5 column elements in each table where table size = 4. To access 1^{st} value of the 1^{st} table use Array_name[0][0][0], to access 2^{nd} row 3^{rd} column value of the 3^{rd} table then use Array_name[2][1][2] and to access the 8^{th} row 5^{th} column of the last table (4^{th} table) then use Array_name[3][7][4]. Lets see the example for better understanding:

**For Example**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
int Employees[2][4][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }, { {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} } }; //To Access the values in the Employees[2][4][3] array //Accessing First Table Rows & Columns Printf("%d", Employees[0][0][0]) = 10 Printf("%d", Employees[0][0][1]) = 20 Printf("%d", Employees[0][0][2]) = 30 Printf("%d", Employees[0][1][0]) = 15 Printf("%d", Employees[0][1][1]) = 25 Printf("%d", Employees[0][1][2]) = 35 Printf("%d", Employees[0][2][0]) = 22 Printf("%d", Employees[0][2][1]) = 44 Printf("%d", Employees[0][2][2]) = 66 Printf("%d", Employees[0][3][0]) = 33 Printf("%d", Employees[0][3][1]) = 55 Printf("%d", Employees[0][3][2]) = 77 //Accessing Second Table Rows & Columns Printf("%d", Employees[1][0][0]) = 1 Printf("%d", Employees[1][0][1]) = 2 Printf("%d", Employees[1][0][2]) = 3 Printf("%d", Employees[1][1][0]) = 5 Printf("%d", Employees[1][1][1]) = 6 Printf("%d", Employees[1][1][2]) = 7 Printf("%d", Employees[1][2][0]) = 2 Printf("%d", Employees[1][2][1]) = 4 Printf("%d", Employees[1][2][2]) = 6 Printf("%d", Employees[1][3][0]) = 3 Printf("%d", Employees[1][3][1]) = 5 Printf("%d", Employees[1][3][2]) = 7 //To Alter the values in the Employees[4][3] array Employees[0][2][1] = 98; // It will change the value of Employees[0][2][1] from 44 to 98 Employees[1][2][2] = 107; // It will change the value of Employees[1][2][2] from 6 to 107 |

For the large number of rows and columns we can access them using For Loop. Say for example, to access array Employees[10][25][60]

1 2 3 4 5 6 7 8 9 10 11 12 |
int tables, rows, columns; for (tables = 0; tables < 10; tables ++) { for (rows = 0; rows < 25; rows++) { for (columns =0; columns < 60; columns++) { Printf(“%d”, Employees[tables][rows][columns]); } } } |

## Multi Dimensional Array in C Example

In this C program, We will declare Three dimensional array and initialize it with some values. Using the For loop we will display each and every individual values present in the array as per the index.

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* Example for Multi Dimensional Array in C programming */ #include<stdio.h> int main() { int tables, rows, columns; int Employees[2][2][3] = { { {9, 99, 999}, {8, 88, 888} }, { {225, 445, 665}, {333, 555, 777} } }; for (tables = 0; tables < 2; tables++) { for (rows = 0; rows < 2; rows++) { for (columns =0; columns < 3; columns++) { printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns, Employees[tables][rows][columns]); } } } return 0; } |

**OUTPUT**

**ANALYSIS**

**Table First Iteration**

The value of tables will be 0 and the condition (tables < 2) is True. So, it will enter into second for loop (Row Iteration)

**Row First Iteration**

The value of row will be 0 and the condition (rows < 2) is True. So, it will enter into third for loop (Column Iteration)

**Column First Iteration**

The value of column will be 0 and the condition (columns < 2) is True. So, it will start executing the statements inside the loop until the condition fails.

1 |
printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns, Employees[tables][rows][columns]); |

Employees[tables][rows][columns] = Employees[0][0][0] = 9

**Column Second Iteration**

The value of column will be 1 and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0

Employees[tables][rows][columns] = Employees[0][0][1] = 99

**Column 3 ^{rd} Iteration**

The value of columns will be 2 and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0

Employees[tables][rows][columns] = Employees[0][0][2] = 999

After the increment, the value of columns will be 3 and the condition (columns < 3) will fail. So it will exit from the 3^{rd} for loop.

Now the value of rows will be incremented and starts the second row iteration

**Row Second Iteration**

The value of row will be 1 and the condition (rows < 2) is True. So, it will enter into second for loop

**Column First Iteration**

The value of column will be 0 and the condition (columns < 3) is True. So, it will start executing the statements inside the loop until the condition fails.

1 |
printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns, Employees[tables][rows][columns]); |

Employees[tables][rows][columns] = Employees[0][1][0] = 8

**Column Second Iteration**

The value of column will be 1 and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0

Employees[tables][rows][columns] = Employees[0][1][1] = 88

**Column 3 ^{rd} Iteration**

The value of columns will be 2 and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0

Employees[tables][rows][columns] = Employees[0][1][1] = 888

After the increment the value of columns will be 3 and the condition (columns < 3) will fail. So it will exit from the 3^{rd} for loop.

Now the value of rows will be incremented, it means rows =2. Condition (rows < 2) will fail So, it will exit from the 2^{nd} For Loop.

Now, the value of tables will be incremented to 1, it means

for (tables = 1; tables < 2; tables++)

Condition is True so it will repeat the above iteration with table value 1. Once completed the value of tables will be incremented.

Now tables = 2 and the condition (tables < 2) will fail so, it will exit from the First for loop.

Thank you for Visiting Our Blog

## Share your Feedback, or Code!!