This section will explain the Three Dimensional Array in C. An Array having more than one dimension is called Multi Dimensional array in C. In our previous article, we discussed Two Dimensional, which is the simplest form of C 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

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

Data_Type Arr_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
- Arr_Name: This is the name you want to give it to Multi Dimensional array in C.
- Tables: It will decide the number of tables one can accept. Two Dimensional is always a single table with rows and columns. In contrast, Multi Dimensional array in C is more than 1 table with rows and columns.
- Row_Size: Number of Row elements it can store. For example, Row_Size =10, the array will have 10 rows.
- Column_Size: Number of Column elements it can store. For example, Column_Size = 8, it 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, the above one will accept only integers. If you try to add float values, it throws an error.
- Employees is the name of the multi dimensional array in c.
- The number of tables = 2. So, it will hold a maximum of 2 levels of data (rows and columns).
- The Row size is 4. It means Employees array only accept 4 integer values as rows.
- If we try to store more than 4, it throws an error.
- We can store less than 4. For Example, If we store 2 integer values, the remaining two will assign with the default value (Which is 0).

- The Column size is 3. It means Employees will only accept 3 integer values as columns.
- If we try to store more than 3, it throws an error.
- We can store less than 3. For Example, If we store 1 integer values, the remaining 2 values will assign with the default value (Which is 0).

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

Please refer to the Array and Two Dimensional articles in C Programming.

## C Multi Dimensional Array Initialization

We can initialize the C Multi Dimensional Array in multiple ways

First Approach of Multi Dimensional Array in C

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 the 1^{st} table holds 4 Rows * 3 Columns, and the 2^{nd} table also holds 4 Rows * 3 Columns

The first three elements of the first table will be 1st row, the second three elements will be 2nd row, the next three elements will be 3rdrow, and the last 3 elements will be 4th 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 to use the curly braces to separate the rows.

Same for the second table.

Second Approach of Multi Dimensional Array in C

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 of a multi dimensional array in c. But, the compiler is intelligent enough to calculate the size by checking the number of elements inside the row.

We can also write

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 for Multi Dimensional Array in C

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 with row size = 4 and column size = 3. But we only assigned 1 column in the 1st row and 2 columns in the 2nd row of the first table. In these situations, the remaining values will assign to default values (0 in this case).

The above one will be:

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

The above 3 ways are good to store a small number of elements into the array. To store 100 rows or 50 column values in 20 tables, we can use the loop concept such as for loop and while loop

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 in C

We can access the C Multi Dimensional array elements using indexes. Index starts at 0 and ends at n-1, where n is the size of a row or column.

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

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] //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] 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 a large number of rows and columns, we can access them using For Loop. Say, for example, to access Employees[10][25][60]

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 every individual value present in it as per the index.

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

Let us see the C Multi Dimensional Array program execution in iteration wise

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)

C Multi Dimensional array Row First Iteration

The value of the 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 the 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.

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 the column will be 1, and the condition (columns < 3) is True. Since we didn’t exit from the Columns loop, the row value will be 0

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

Column 3^{rd} Iteration

The value of the columns will be 2, and the condition (columns < 3) is True.

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

After incrementing, the value of the 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 increment and starts the second row iteration

Row Second Iteration

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

Multi Dimensional Array in C Column First Iteration

The value of the column will be 0, and the condition (columns < 3) is True.

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

Column Second Iteration

column = 1, and the condition (columns < 3) is True. Still, the table value will be 0

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

Column 3^{rd} Iteration

columns = 2, and the condition (2 < 3) is True.

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

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

Next, the value of the row incremented. It means rows = 2, and the condition (2 < 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 the tables will increment.

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

Comments are closed.