BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Multi-Dimensional Array in C: 3D Arrays & Memory Mapping | Unit 4 | BKNMU BCA

by BCA School •

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

BKNMU Junagadh BCA Sem 1 Unit 4 3D Array Multi-Dimensional Hierarchical Indexing
In simple words: An array having more than two dimensions is known as a Multi-Dimensional Array. The most common and practical higher-dimension structure is the Three-Dimensional Array (3D Array). You can visualize a 3D array as a "book of matrices" or a collection of 2D tables stacked in depth (layers or blocks). Each element is indexed via three coordinates: arr[block][row][col]. This guide explains declaration, memory formulas, and traversal for BKNMU BCA Semester 1 (Unit 4).
📖 Structural Dimensions of Multi-Dimensional Arrays

Multi-dimensional concepts are categorized into four structural aspects:

3 Subscripts

1. Block-Row-Col Layout

Accessed through three bracketed indices: arr[b][r][c], pointing to the page/block, row position, and column offset.

Product Sizing

2. Total Element Capacity

The total capacity equals the mathematical product of all dimensions: Size1 × Size2 × Size3 × ... × SizeN.

Linear Memory

3. Row-Major Flattening

Physical RAM is strictly linear (1D). C flattens higher dimensions contiguously, storing full blocks in succession.

Triple Loops

4. Multi-Level Traversal

Processing 3D structures requires 3 nested for loops: outermost for blocks, middle for rows, and innermost for columns.

1️⃣ Master Table: Syntax, Representation & Initialization

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)$.
ℹ️ Mathematical Memory Formulas for 3D Arrays:
  • 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 (where int is 4 bytes): 2 × 3 × 4 = 24 elements, consuming 24 × 4 = 96 Bytes.
💻 Practical Code Example: 3D Array Input and Block Display

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;
}
🔄 Step-by-Step Flow: Memory Mapping for a 3D Array
1

Base Address Mapping

The array name points to the address of the very first element: &arr[0][0][0]. Memory in RAM remains contiguous.

2

Physical Offset Formula

To reach arr[b][r][c], compiler calculates address as: Address = Base + [ (b × ROWS × COLS) + (r × COLS) + c ] × sizeof(type).

3

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.

💡 Real-World Mental Model for a 3D Array: Think of a 3D array as a school notebook:
  • 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.
⚠️ Memory Explosion Risk: Each extra dimension multiplies the total memory footprint exponentially. An array like 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.
📝 Quick Revision — Key Exam Points
  • 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 for loops.