C 2-D Array
Syntax
datatype array_name[row_size][column_size];
Example
#include <stdio.h>
int main() {
int my_array[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", my_array[i][j]);
}
printf("\n");
}
return 0;
}
Output
1 2 3
4 5 6
Explanation
A two-dimensional (2-D) array is an array of arrays, where each array is itself an array of elements of a certain datatype. For example, a 2-D array could be declared as int array[2][3]
, meaning it has 2 rows and 3 columns, with each element being of type int
.
Use
2-D arrays are commonly used in matrix manipulation, image processing, and game development. They can be used to store and manipulate large amounts of data in a tabular format.
Important Points
- A 2-D array can be initialized using nested braces containing the initial values for each element in row-major order.
- The number of rows and columns in a 2-D array must be specified at the time of declaration and cannot be changed dynamically during runtime.
- Elements of a 2-D array can be accessed using two indices, the first for the row and the second for the column.
Summary
A 2-D array is an array of arrays, commonly used to store and manipulate tabular data in programming. Declaration of a 2-D array requires specification of both the row size and column size, which cannot be changed dynamically during runtime. Understanding the syntax, initialization, and indexing of 2-D arrays is an important skill for developers working on larger-scale data manipulation and processing projects.