Multi-Dimensional Array in C: 3D Arrays & Memory Mapping | Unit 4 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 4
Multi-Dimensional Array in C: 3D Arrays, Memory & Indexing
Complete Guide to Higher Dimensions, Block-Row-Column Mental Models, Linear RAM Mapping, and Nested Loops
arr[block][row][col]. This guide explains declaration, memory formulas, and traversal for BKNMU BCA Semester 1 (Unit 4).
Multi-dimensional concepts are categorized into four structural aspects:
1. Block-Row-Col Layout
Accessed through three bracketed indices: arr[b][r][c], pointing to the page/block, row position, and column offset.
2. Total Element Capacity
The total capacity equals the mathematical product of all dimensions: Size1 × Size2 × Size3 × ... × SizeN.
3. Row-Major Flattening
Physical RAM is strictly linear (1D). C flattens higher dimensions contiguously, storing full blocks in succession.
4. Multi-Level Traversal
Processing 3D structures requires 3 nested for loops: outermost for blocks, middle for rows, and innermost for columns.
A complete architectural reference of 3D array declarations and initialization patterns:
| Declaration Pattern | Syntax Example | Conceptual Layout & Storage |
|---|---|---|
| Uninitialized 3D Declaration | int arr[2][3][4]; |
Allocates 2 separate tables, each with 3 rows and 4 columns. Holds garbage values. |
| Hierarchical Initialization | int a[2][2][2] = {{{1,2},{3,4}}, {{5,6},{7,8}}}; |
Structured block-by-block using nested curly braces. Recommended for exam clarity. |
| Flat List Initialization | int a[2][2][2] = {1, 2, 3, 4, 5, 6, 7, 8}; |
Fills linear memory sequentially according to row-major order across blocks. |
| Accessing Element | val = arr[1][0][2]; |
Fetches item from Block 1, Row 0, Column 2 directly in constant time $O(1)$. |
- Total Elements:
Total = Blocks × Rows × Columns - Total Memory Bytes:
Total Bytes = Blocks × Rows × Columns × sizeof(Data_Type) - Example: For
int arr[2][3][4];on modern GCC (whereintis 4 bytes):2 × 3 × 4 = 24 elements, consuming24 × 4 = 96 Bytes.
The following program demonstrates initializing a $2 \times 2 \times 3$ array (2 blocks, 2 rows, 3 columns) and displaying each block as an independent matrix:
#include <stdio.h>
int main() {
// Declare 3D array: 2 blocks, 2 rows, 3 columns
int test[2][2][3];
int b, r, c;
// 1. Reading elements using 3 nested loops
printf("Enter 12 integer values (2 blocks of 2x3):\n");
for (b = 0; b < 2; b++) {
for (r = 0; r < 2; r++) {
for (c = 0; c < 3; c++) {
printf("Item [%d][%d][%d]: ", b, r, c);
scanf("%d", &test[b][r][c]);
}
}
}
// 2. Displaying blocks sequentially
printf("\n--- Displaying 3D Array Blocks ---\n");
for (b = 0; b < 2; b++) {
printf("\n=== BLOCK (Matrix) %d ===\n", b);
for (r = 0; r < 2; r++) {
for (c = 0; c < 3; c++) {
printf("%d\t", test[b][r][c]);
}
printf("\n");
}
}
return 0;
}
Base Address Mapping
The array name points to the address of the very first element: &arr[0][0][0]. Memory in RAM remains contiguous.
Physical Offset Formula
To reach arr[b][r][c], compiler calculates address as: Address = Base + [ (b × ROWS × COLS) + (r × COLS) + c ] × sizeof(type).
Linear Sequential Traversal
Hardware accesses memory in sequence: Block 0 Row 0, then Block 0 Row 1, then progresses to Block 1 Row 0 without memory gaps.
- Dimension 1 (Block): Page number in the notebook.
- Dimension 2 (Row): Line number on that specific page.
- Dimension 3 (Column): Word or column offset on that line.
int data[50][50][50]; allocates 125,000 integers (500 KB on GCC), which can cause stack overflows if declared inside a local function block.
- Definition: An array having 3 or more dimensions (subscripts).
- Common Form: 3D Array:
arr[blocks][rows][columns]. - Total Size: Product of all dimensions multiplied by element byte size.
- Memory Storage: Stored contiguously in RAM using hierarchical Row-Major Order.
- Traversal: Handled using 3 nested
forloops.