Multi-dimensional arrays
In C programming, multi-dimensional arrays are arrays that have more than one dimension. They are commonly used to represent matrices, tables, or other tabular data structures.
To declare and access elements in a multi-dimensional array, you can use multiple sets of brackets [].
Here's an example of a two-dimensional array in C:
#include <stdio.h>
int main() {
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Accessing array elements
printf("The element at matrix[0][0] is: %d\n", matrix[0][0]);
printf("The element at matrix[1][2] is: %d\n", matrix[1][2]);
printf("The element at matrix[2][3] is: %d\n", matrix[2][3]);
return 0;
}
Output:
The element at matrix[0][0] is: 1
The element at matrix[1][2] is: 7
The element at matrix[2][3] is: 12
In this example, we declare a two-dimensional integer array `matrix` with 3 rows and 4 columns. We initialize it with some values. To access elements in the array, we use the notation `matrix[row_index][column_index]`, where `row_index` is the index of the desired row, and `column_index` is the index of the desired column.