C Notes
Complete guide to 2D arrays in C including declaration, initialization, matrix operations, row-column access patterns, and practical examples with memory diagrams.
A two-dimensional array is essentially an array of arrays — it organizes data in a tabular format with rows and columns. If you think of a 1D array as a single row of boxes, a 2D array is a grid or matrix of boxes arranged in rows and columns.
2D arrays are everywhere in programming: storing matrices for mathematical operations, representing game boards, managing tabular data like spreadsheets, and handling image pixel data.
Declaration of 2D Arrays
The general syntax for declaring a two-dimensional array is:
Examples:
Memory Layout: Row-Major Order
Although we think of 2D arrays as grids, memory is linear. C stores 2D arrays in row-major order — all elements of row 0 come first, then row 1, and so on.
Initialization Methods
Method 1: Nested Braces (Recommended)
Method 2: Flat Initialization
Method 3: Partial Initialization
Method 4: Omitting Row Size
You can omit the first dimension (rows) when initializing, but the second dimension (columns) is always required:
Accessing and Modifying Elements
Element at [1][2] = 6 Modified [0][0] = 100
Reading and Displaying a 2D Array
Enter elements of 3x3 matrix: [0][0]: 1 [0][1]: 2 [0][2]: 3 [1][0]: 4 [1][1]: 5 [1][2]: 6 [2][0]: 7 [2][1]: 8 [2][2]: 9 Matrix: 1 2 3 4 5 6 7 8 9
Matrix Operations
Matrix Addition
Sum of matrices: 8 10 12 14 16 18
Matrix Multiplication
Product: 58 64 139 154
Matrix Transpose
Original: 1 2 3 4 5 6 7 8 9 Transpose: 1 4 7 2 5 8 3 6 9
Row-wise and Column-wise Operations
Row sums: 10 26 42 Col sums: 15 18 21 24
Diagonal Elements
For square matrices, accessing diagonal elements is a common operation:
Primary diagonal: 1 5 9 Secondary diagonal: 3 5 7
Interview Questions
Q1: Why must the column size be specified when declaring a 2D array in C?
The compiler needs the column count to calculate memory addresses. The address formula base + (i × cols + j) × sizeof(type) requires knowing cols. Without it, the compiler cannot determine where each row begins.
Q2: What's the difference between row-major and column-major order?
In row-major order (used by C), elements of each row are stored contiguously. In column-major order (used by Fortran), elements of each column are stored contiguously. This affects cache performance when traversing matrices.
Q3: How do you pass a 2D array to a function in C?
You must specify at least the column size: void func(int arr[][4], int rows). Alternatively, use a pointer: void func(int (*arr)[4], int rows). The row count is typically passed as a separate parameter.
Q4: Can you have a 2D array with different row lengths (jagged array) in C?
Not with static 2D arrays. However, you can simulate jagged arrays using an array of pointers, where each pointer points to a dynamically allocated row of different length.
Summary
- 2D arrays store data in a matrix format with rows and columns
- C uses row-major order for memory storage
- Column size must always be specified in declarations
- Address formula:
base + (i × cols + j) × sizeof(type) - Common operations: addition, multiplication, transpose, diagonal access
- Nested loops are the standard pattern for traversing 2D arrays
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Two Dimensional Arrays in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, arrays, two, dimensional, two dimensional arrays in c programming
Related C Programming Topics