Multidimensional Arrays in C

In C programming language, a multidimensional array is an array that contains one or more arrays as its elements. Multidimensional arrays are useful for storing and accessing data that has multiple dimensions, such as matrices and tables.

To declare a multidimensional array in C, you need to specify the data type of the elements and the size of each dimension. Here’s an example of how to declare and initialize a two-dimensional array in C:

int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};

In this example, we declare a two-dimensional integer array named “arr” that has 3 rows and 4 columns. We initialize the array with the values 1 through 12.

We can access the elements of a multidimensional array using nested array indices. The first index specifies the row number, and the second index specifies the column number. Here’s an example of how to access the elements of a two-dimensional array:

int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};

printf("%d\n", arr[0][0]); // prints 1
printf("%d\n", arr[1][2]); // prints 7
printf("%d\n", arr[2][3]); // prints 12

In this example, we declare a two-dimensional integer array named “arr” and initialize it with the values 1 through 12. We then use nested array indices to access the elements of the array.

We can also use nested loops to iterate over the elements of a multidimensional array. Here’s an example of how to use nested loops to iterate over the elements of a two-dimensional array:

int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
int i, j;

for (i = 0; i < 3; i++) {
    for (j = 0; j < 4; j++) {
        printf("%d ", arr[i][j]);
    }
    printf("\n");
}

In this example, we declare a two-dimensional integer array named "arr" and initialize it with the values 1 through 12. We then use nested loops to iterate over the elements of the array and print them to the console.

Multidimensional arrays in C can have any number of dimensions, but they can become difficult to manage as the number of dimensions increases. However, multidimensional arrays are a powerful feature of C programming language that enable the programmer to store and access data with multiple dimensions efficiently.