C Multidimensional Arrays

C Tutorial

📊 C Multidimensional Arrays (Beginner → Advanced)

In C language, a multidimensional array is an array that has more than one dimension.
The most common type is the 2D array, widely used for matrices, tables, images, and DSA problems.


1️⃣ What is a Multidimensional Array?

An array with rows and columns (or more dimensions).

Syntax (2D Array)

data_type array_name[rows][columns];

Example

int matrix[3][4];

✔ 3 rows
✔ 4 columns


2️⃣ Declaring & Initializing 2D Arrays

Method 1: At Declaration


 


Method 2: Single-Line Initialization

int a[2][3] = {1, 2, 3, 4, 5, 6};

✔ Filled row-wise


3️⃣ Accessing Elements of 2D Array

printf("%d", a[1][2]); // Output: 6

📌 Index starts from 0


4️⃣ Traversing 2D Array Using Nested Loops ⭐


 

✔ Uses nested for loops


5️⃣ Taking User Input in 2D Array


 

✔ Common in exams & interviews


6️⃣ Passing 2D Array to Function ⭐ (Very Important)

Correct Way


 

📌 Column size is mandatory


7️⃣ Using Pointer with 2D Array 🔥

int a[2][3];
int (*p)[3] = a;
printf(“%d”, p[1][2]); // same as a[1][2]

✔ Used in advanced memory handling


8️⃣ Matrix Operations (Interview Favorite ⭐)

Matrix Addition


 


Matrix Multiplication (Basic)



 


9️⃣ 3D Arrays (Advanced)

int a[2][3][4];

Access:

a[i][j][k];

✔ Used in graphics & scientific computing


🔟 Memory Representation of 2D Array ⭐

Stored in row-major order:

int a[2][3] = {1,2,3,4,5,6};

Memory:

1 2 3 4 5 6

1️⃣1️⃣ Common Mistakes ❌

❌ Forgetting column size in function
❌ Accessing out-of-bounds index
❌ Confusing pointer-to-array with array-of-pointers
❌ Wrong loop limits


📌 Interview Questions (Must Prepare)

  1. What is a multidimensional array?

  2. How 2D arrays are stored in memory?

  3. Why column size is mandatory in function?

  4. How to pass 2D array to function?

  5. Difference between int a[3][4] and int **a?


🔥 Real-Life Use Cases

  • Matrix calculations

  • Image processing

  • Game boards

  • Embedded sensor data

  • DSA (DP, grids)


✅ Summary

✔ Multidimensional arrays store data in rows & columns
✔ 2D arrays are most common
Nested loops are used for traversal
✔ Column size is mandatory in function parameters
✔ Very important for DSA & interviews

You may also like...