Syntax of Two Dimensional(2D) Array in C
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.
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;
}
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;
}
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.
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.
Check out this problem - Search In Rotated Sorted Array