C 2D & Multi-Dimensional Arrays: Row-Major RAM Mapping & Matrix Mathematics

โšก C (C17 / C23 Standard) ๐ŸŸข Lesson 15 ๐Ÿ“‚ Phase 07: Arrays & Memory Organization ๐Ÿ“… 2026 Comprehensive Edition
๐Ÿ“Œ Covered in this in-depth guide: 2D/3D Array Architecture ยท Row-Major Memory Mapping Formula ยท Matrix Addition & Transpose ยท Array of Characters vs Strings ยท Multi-Dimensional Indexing

Welcome to Phase 7 (Chapter 15): C 2D & Multi-Dimensional Arrays, Row-Major RAM Mapping & Matrix Mathematics Masterclass! When software systems model mathematical matrices, tabular spreadsheets, graphic coordinate maps, game boards (such as Chess, Go, or Tic-Tac-Toe), or multidimensional physics tensors, single-dimensional arrays are insufficient. Multi-Dimensional Arrays provide the architectural abstraction to organize data across rows and columns. In this comprehensive guide, you will master how physical computer hardware flattens multi-dimensional grids into linear 1D Row-Major RAM memory bytes, implement core linear algebra algorithms (Matrix Addition and Matrix Transposition), and compare character arrays with null-terminated C strings.

12D Array Abstraction vs Physical Row-Major Order in RAM

Programmer conceptualizes a 2D array as a Grid table with Rows and Columns (e.g. $2 \times 3$ matrix). Kaani physical computer RAM is strictly a single, continuous, linear 1D sequence of byte addresses!

C language compilers organize multi-dimensional arrays in RAM using Row-Major Order: Row 0 is placed in memory first, followed immediately by Row 1, then Row 2, without any gaps.

Physical RAM Flattening: int matrix[2][3] = {{10, 20, 30}, {40, 50, 60}};

Conceptual 2D Grid: Physical 1D Linear RAM Memory Sequence:
Row 0: [ 10 ] [ 20 ] [ 30 ] โ”€โ”€โ”€โ–บ [ 10 ][ 20 ][ 30 ] [ 40 ][ 50 ][ 60 ]
Row 1: [ 40 ] [ 50 ] [ 60 ] โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Row 0 Bytes Row 1 Bytes

RAM Address: 0x3000 0x3004 0x3008 0x300C 0x3010 0x3014
Stored Value: 10 20 30 40 50 60

๐Ÿ“ The Mathematical 2D Address Calculation Formula:

$$\text{Address of } matrix[i][j] = \text{Base Address} + \Big( (i \times \text{Total Columns}) + j \Big) \times \text{sizeof(element)}$$
โ€ข i * Total Columns skips all the previous full rows in memory.
โ€ข + j moves to the target column offset within the current row.
โ€ข Multiplying by sizeof(element) converts the element count into exact physical RAM byte offsets!

2Matrix Mathematics: Matrix Addition & Matrix Transpose

๐Ÿ“ 1. Matrix Addition ($C[i][j] = A[i][j] + B[i][j]$)

Rendu matrices ni add cheyyalante, vatiki exact same dimensions $(M \times N)$ undali. Outer loop rows ni, inner loop columns ni iterate chesthu corresponding element values ni add chesthamu.

๐Ÿ”„ 2. Matrix Transposition ($T[j][i] = M[i][j]$)

Matrix Transpose ante Rows ni Columns ga, Columns ni Rows ga convert cheyyadam. Original matrix dimension $(M \times N)$ ayithe, transposed matrix dimension $(N \times M)$ ga maruthundhi.

C โ€” 2D Matrix Addition & Transpose Algorithms โ–ถ Run Code
#include <stdio.h>

#define ROWS 2
#define COLS 3

int main(void) {
    int A[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}};
    int B[ROWS][COLS] = {{7, 8, 9}, {1, 2, 3}};
    int Sum[ROWS][COLS];
    int Transpose[COLS][ROWS];

    // 1. Matrix Addition
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            Sum[i][j] = A[i][j] + B[i][j];
        }
    }

    // 2. Matrix Transposition of A (2x3 -> 3x2)
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            Transpose[j][i] = A[i][j];
        }
    }

    printf("--- Matrix Sum (A + B) ---\n");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) printf("%3d ", Sum[i][j]);
        printf("\n");
    }

    printf("\n--- Transpose of Matrix A (3x2) ---\n");
    for (int i = 0; i < COLS; i++) {
        for (int j = 0; j < ROWS; j++) printf("%3d ", Transpose[i][j]);
        printf("\n");
    }

    return 0;
}
3Array of Characters vs Null-Terminated Strings
AttributeRaw Character Array (char arr[])Null-Terminated C String (char str[])
Null Terminator ('\0')โŒ NOT guaranteed unless manually placed.โœ… Compulsory automatically appended at end.
Standard I/O CompatibilityCannot be safely printed with %s.Fully compatible with printf("%s") & string.h.
Memory Sizechar ch[2] = {'A', 'B'}; $ ightarrow$ Takes 2 Bytes.char str[3] = "AB"; $ ightarrow$ Takes 3 Bytes ('A', 'B', '\0').
๐Ÿ’ป Try It Yourself โ€” Test 2D Arrays in C Compiler

Run this 2D identity matrix generator in our live GCC compiler:

C (GCC Standard) โ–ถ Open C Compiler
#include <stdio.h>

int main(void) {
    int n = 3;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", (i == j) ? 1 : 0);
        }
        printf("\n");
    }
    return 0;
}
Open in Online C Compiler โ†’