Syntax of 2D Array
The syntax of a two-dimensional array is similar to that of a one-dimensional array, but here is the only change it has two subscripts. The syntax of the two-dimensional array is given below:
data_type array_name[row_size][column_size];
In the above syntax, row_size specifies the number of rows, and column_size specifies the number of columns in the array. The total number of elements in the above-declared array is row_size*column_size. Consider we have an array two_dimen declared as-
int two_dimen[5][8];
Here, two_dimen is the array's name, 5 is the number of rows, and 8 is the number of columns.
Types of Multidimensional Arrays
Let's discuss the type of multidimensional array we have:
1. Two-Dimensional Arrays
A two-dimensional array is the most common type of multidimensional array. It consists of elements arranged in a tabular format with rows and columns. Each element in a 2D array is accessed using two indices, one for the row and another for the column. For example, a 2D array named `arr` with 3 rows and 4 columns can be declared as `int arr[3][4]`. Two-dimensional arrays are widely used to represent matrices, tables, or grids.
2. Three-Dimensional Arrays
A three-dimensional array extends the concept of a 2D array by adding an extra dimension. It can be visualized as multiple 2D arrays stacked on top of each other. Each element in a 3D array is accessed using three indices: one for the depth, one for the row, and one for the column. For example, a 3D array named `arr` with 2 depths, 3 rows, and 4 columns can be declared as `int arr[2][3][4]`. Three-dimensional arrays are useful in scenarios where data needs to be organized in a cubic or volumetric manner, such as representing 3D objects or multiple layers of data.
3. Higher-Dimensional Arrays
Multidimensional arrays can extend beyond three dimensions. While less common, it is possible to have arrays with four, five, or even more dimensions. The number of indices required to access an element in a higher-dimensional array corresponds to the number of dimensions. For example, a four-dimensional array named `arr` with dimensions 2, 3, 4, and 5 can be declared as `int arr[2][3][4][5]`. Higher-dimensional arrays are used in specialized applications where data needs to be organized in complex structures.
4. Jagged Arrays
Jagged arrays, also known as arrays of arrays, are a special type of multidimensional array where the length of each sub-array can vary. In a jagged array, each element of the main array is itself an array, and these sub-arrays can have different sizes. For example, a jagged array named `arr` with 3 rows can be declared as `int *arr[3]`, where each element of `arr` is a pointer to an integer array. The sub-arrays can be dynamically allocated with different lengths. Jagged arrays offer flexibility when dealing with irregular or non-uniform data structures.
Loop Through a 2D Array in C
To loop through a 2D array in C, use nested for loops: the outer loop iterates through rows, and the inner loop iterates through columns.
Example
C
#include <stdio.h>
int main() {
int array[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
for (int i = 0; i < 3; i++) { // Loop through rows
for (int j = 0; j < 4; j++) { // Loop through columns
printf("%d ", array[i][j]);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
1 2 3 4
5 6 7 8
9 10 11 12
Declaration of Two Dimensional (2D) Array in C
1. Integer 2D Array
int matrix[3][4];
This declaration creates a 2D array named matrix with 3 rows and 4 columns, capable of holding integer values.
2. Float 2D Array
float table[5][6];
This creates a 2D array named table with 5 rows and 6 columns for floating-point values.
3. Character 2D Array
char letters[2][3];
This creates a 2D array named letters with 2 rows and 3 columns for character values.
Initialization of Two Dimensional (2D) Array in C
In the one-dimensional (1D) array, we don't need to specify the size of the array if the array is declared and initialized simultaneously. However, this will not work with two-dimensional (2D) arrays. We will have to define at least one (second) dimension of the array. There are two methods to initialize a two-dimensional array during declaration. Both methods only differ syntactically.
First method
int arr[3][3]={{10, 11, 12}, {15, 14, 12}, {25, 64, 22}};
Second method
int arr[3][3]={10, 11, 12, 15, 14, 12, 25, 64, 22};
In both methods, the array has 3 rows and 3 columns. In the first method, each set of inner braces represents one row. In the second method, the elements will be filled in the array in order, the first 3 elements from the left in the first row, the next 3 elements in the second row, and so on. Generally, the first initialization method is preferred as we can clearly understand and visualize the rows and columns of two dimensional(2D) array.
Accessing Elements of a Two-dimensional (2D) Array
Elements in Two-Dimensional(2D) arrays are accessed using the subscripts, i.e., row index and column index of the array.
For example:
int data=arr[3][2];
The above statement will take the 3rd element from the 4th row of the array.
Example of Two-Dimensional (2d) array in C
Here is an example of a two-dimensional array:
C
#include<stdio.h>
int main()
{
/* an array with 3 rows and 3 columns*/
int arr[3][3]={{10, 11, 12}, {15, 14, 12}, {25, 64, 22}};
int i, j;
/* output each array element's value */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Element at arr[%d][%d]: %d",i,j,arr[i][j]);
printf("\n");
}
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Element at arr[0][0]: 10
Element at arr[0][1]: 11
Element at arr[0][2]: 12
Element at arr[1][0]: 15
Element at arr[1][1]: 14
Element at arr[1][2]: 12
Element at arr[2][0]: 25
Element at arr[2][1]: 64
Element at arr[2][2]: 22
In the above example, we used a nested 'for' loop to print or traverse all the two-dimensional(2D) array elements. We used the First 'for' loop to traverse the rows and another to traverse columns.
How 2D Arrays are Stored in Memory?
- Row-Major Order: In C, 2D arrays are stored in memory in row-major order. This means that the entire row is stored in contiguous memory locations before moving to the next row.
- Contiguous Allocation: All elements of a row are stored sequentially in memory. After the last element of a row, the first element of the next row is stored immediately.
- Single Block of Memory: A 2D array is essentially a single block of memory, and accessing an element involves calculating the correct offset based on the row and column indices.
Example:
Consider the following 2D array:
int array[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Memory Layout:
- The elements are stored in memory as follows:
- 1, 2, 3 (Row 0)
- 4, 5, 6 (Row 1)
In memory, the array is laid out like this:
1 2 3 4 5 6
You can also read about dynamic array in C.
Frequently Asked Questions
How to calculate 2D array size in C?
In C, the size of a 2D array can be calculated using the formula rows * columns * sizeof(data_type), where data_type is the type of elements in the array.
How many loops do we use to traverse a two-dimensional array?
We use two loops to traverse a two-dimensional array.
What is a 3-dimensional array?
A 3-dimensional array is a multi-dimensional array with three indices. It can be visualized as an array of 2D arrays, or a grid of grids. It is used to store data in a three-dimensional format, which can be useful for representing data structures like a stack of matrices or a 3D space.
What is a 2-dimensional single array?
A 2-dimensional single array, often referred to simply as a 2D array, is a flat, single block of memory that stores elements in a grid-like structure with rows and columns. Internally, it is represented as a single-dimensional array with a specific layout that mimics a 2D matrix.
What is the use of a two-dimensional(2D) array in C?
The use of a two-dimensional(2D) array in C are:
1. The two-dimensional array is used to store a list of arrays with similar data types.
2. Two-dimensional arrays are used to represent matrices.
How do you write a 2D array?
A 2D array stores data in rows and columns. In Python, it’s a list of lists like array = [[1, 2], [3, 4]]. In C++, declare as int array[2][2] = {{1, 2}, {3, 4}}.
Conclusion
In this blog, we learned about Multidimensional Array in C (Two-Dimensional Array). We've explored the fundamentals of multidimensional arrays in C, focusing specifically on two-dimensional arrays. We’ve learned that a 2D array can be visualized as a grid or matrix, where elements are accessed using two indices—rows and columns. Understanding how these arrays are stored in memory, in row-major order, helps us appreciate their efficient access patterns and manipulation.
You can also check out our other blogs on Code360.