If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C programming language, a multidimensional array is an array that contains more than one dimension. The most common types of multidimensional arrays are two-dimensional and three-dimensional arrays.
A two-dimensional array is an array of arrays, where each element in the array is an array of elements. Here is an example of how to declare and initialize a 2D array in C:
Copy code
int array2D[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
In this example, array2D
is a 2D array with 3 rows and 4 columns. The elements of the array can be accessed using the indices of the rows and columns, for example:
Copy code
printf("%d", array2D[1][2]); // Prints 7
A three-dimensional array is an array of arrays of arrays, where each element in the array is an array of arrays. Here is an example of how to declare and initialize a 3D array in C:
Copy code
int array3D[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};
In this example, array3D
is a 3D array with 2 planes, 3 rows and 4 columns. The elements of the array can be accessed using the indices of the planes, rows and columns, for example:
Copy code
printf("%d", array3D[1][2][3]); // Prints 24
It's important to note that when you declare a multidimensional array, you must specify the size of each dimension, and that the size of the first dimension is the number of elements in the second dimension and so on.
Comments: 0