A single-dimensional array is a list of elements of the same type stored in contiguous memory locations. It is also known as a one-dimensional array.
int arr[5];
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
A 2D array is an array of arrays. It can be visualized as a matrix with rows and columns.
int matrix[3][4];
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
}
Multi-dimensional arrays can have more than two dimensions. Mostly used are 3D arrays which can be visualized as a collection of 2D matrices.
int cube[2][3][4];
int cube[2][2][2] = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", cube[i][j][k]);
}
}
}