A 2D array in C is an array made of rows and columns. It allows a program to store values in table form instead of a single straight list. If a one-dimensional array stores data like a line, a two-dimensional array stores data like a grid. This makes it useful for matrices, marks tables, game boards, image-like data, and many other structured problems.
Many beginners first learn simple arrays and then get confused when they see declarations such as int matrix[3][4];. The idea becomes much easier once you understand that this means 3 rows and 4 columns of integers stored in contiguous memory. In this article, we will understand 2D array in C, its syntax, initialization, memory layout, traversal, input and output, passing to functions, and common mistakes.
What is 2D Array in C?
A 2D array in C is an array of one-dimensional arrays. In practical terms, it stores elements in row and column form. Each element is accessed using two indices: one for the row and one for the column.
For example, matrix[1][2] means the element at row index 1 and column index 2.
A 2D array in C is basically a table of same-type values stored row by row in memory.
Syntax of 2D Array in C
The general syntax is:
data_type array_name[rows][columns];Example:
int matrix[3][4];This declaration creates a 2D array with 3 rows and 4 columns, so the total number of elements is 3 × 4 = 12.
| Part | Meaning |
|---|---|
data_type | Type of each element such as int or float |
rows | Total number of horizontal groups |
columns | Total number of elements in each row |
Declaration and Initialization of 2D Array in C
A 2D array can be declared first and filled later, or it can be initialized directly at the time of declaration.
Direct initialization example:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};In this example, the array has 2 rows and 3 columns. The first row contains 1, 2, 3 and the second row contains 4, 5, 6.
Another valid form is:
int matrix[][3] = {
{1, 2, 3},
{4, 5, 6}
};Here, the compiler can determine the number of rows, but the number of columns must still be known.
How Elements are Accessed in 2D Array in C
Each element is accessed using two indices:
array_name[row_index][column_index]Example:
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
printf("%d", matrix[1][2]);This prints 60 because matrix[1][2] refers to the second row and third column.
Row and Column Indexing in 2D Array
Like normal arrays in C, indexing in a 2D array starts from 0.
- First row index is
0. - First column index is
0. - Last valid row index is
rows - 1. - Last valid column index is
columns - 1.
If you access an element outside the valid row or column range, the program enters undefined behavior, just like with a normal array.
Memory Representation of 2D Array in C
A very important point is that a 2D array in C is stored in contiguous memory, row by row. This is called row-major order.
Consider this array:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};In memory, the values are stored in this order:
123456
| Element | Logical position | Storage order |
|---|---|---|
matrix[0][0] | row 0, col 0 | 1st |
matrix[0][1] | row 0, col 1 | 2nd |
matrix[0][2] | row 0, col 2 | 3rd |
matrix[1][0] | row 1, col 0 | 4th |
matrix[1][1] | row 1, col 1 | 5th |
matrix[1][2] | row 1, col 2 | 6th |
This row-major storage model is important when working with pointers, function parameters, and memory calculations.
Input and Output of 2D Array in C
To read and print all elements of a 2D array, nested loops are usually used. One loop handles rows and the inner loop handles columns.
#include <stdio.h>
int main(void)
{
int matrix[2][2];
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}This is the standard pattern for reading and printing matrix-style data in C.
Nested Loops in 2D Array in C
A 2D array is one of the best examples for understanding nested loops. The outer loop moves across rows, while the inner loop moves across columns.
That is why topics such as nested for loop and 2D array are usually taught together.
| Loop level | Usually controls |
|---|---|
| Outer loop | Rows |
| Inner loop | Columns |
Passing 2D Array to Function in C
Passing a 2D array to a function is slightly more restrictive than passing a normal array. The number of columns usually must be specified in the function parameter.
#include <stdio.h>
void printMatrix(int matrix[][3], int rows)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}The compiler needs the column size so it can calculate the correct memory offset for each element.
2D Array vs 1D Array in C
| Point | 1D Array | 2D Array |
|---|---|---|
| Index count | One index | Two indices |
| Shape | Linear list | Rows and columns |
| Example | int a[5]; | int a[3][4]; |
| Common use | Simple list of values | Tables, matrices, grids |
A 2D array is not a different data type. It is just an array whose elements are themselves arrays.
Common Mistakes with 2D Array in C
- confusing row count with column count
- using wrong loop limits
- forgetting that indexing starts from
0 - passing a 2D array to a function without defining the column size
- assuming rows are stored separately instead of contiguously
- accessing elements outside valid row or column range
| Mistake | Problem | Better practice |
|---|---|---|
| Wrong column size in function parameter | Incorrect memory access | Specify the correct column count |
Using i < columns for rows | Loop logic breaks | Track rows and columns separately |
| Out-of-bounds access | Undefined behavior | Stay within valid indices |
| Confusing row-major order | Pointer reasoning becomes wrong | Remember C stores 2D arrays row by row |
Best Practices for 2D Array in C
- Use clear variable names such as
matrix,table, ormarks. - Always keep row and column sizes clear in your logic.
- Use nested loops carefully when reading or printing values.
- Remember that C stores 2D arrays in row-major order.
- When passing a 2D array to a function, define the column count correctly.
- Do not access invalid row or column indices.
FAQs
What is 2D array in C?
A 2D array in C is an array of arrays that stores values in row and column form.
How do you declare a 2D array in C?
You declare it using syntax like int matrix[3][4];, where 3 is the number of rows and 4 is the number of columns.
How are 2D arrays stored in C?
They are stored in contiguous memory in row-major order, which means one complete row is stored before the next row.
Why are nested loops used with 2D arrays?
Nested loops are used because one loop handles rows and the inner loop handles columns.
Can we pass a 2D array to a function in C?
Yes, but the column size usually must be specified in the function parameter.
What is the difference between 1D array and 2D array in C?
A 1D array uses one index and stores a linear list, while a 2D array uses two indices and stores data like rows and columns.