Concept and Usage of Multidimensional Arrays in C Language

Previously, we have used basic data objects as elements of an array, such as an array A, which has elements of type int and a total of 10 elements. We can declare such an array using int A[10].Now, let’s expand our thinking: can we use an array as an element of another array? For example, if we have an array B, where the element type is an array containing 10 int elements, and the total number of elements is 5, how should we declare it?1. Array as an Element of an ArrayFirst, let’s review the formula for declaring an array:Concept and Usage of Multidimensional Arrays in C Language

The declaration of an array consists of the array name, element type, and the number of elements. Let’s try to write a declaration for array B:

1. Array Name:B

2. Element Type:int[10]

3. Number of Elements:: 5

Combining these three elements, we can write the following declaration: int[10] B[5];

We still need to move the brackets on the left side of the array name to the far right. int B[5][10]; This is the declaration of array B (the B array contains 5 elements, each of which is an array containing 10 int elements).

Concept and Usage of Multidimensional Arrays in C Language

2. Representation of Two-Dimensional Arrays

The formula for accessing a specific element in an array is as follows: use the form array name[index] to access elements within the array:

Concept and Usage of Multidimensional Arrays in C Language

For example: B[0], each element of array B is an array of type int[10].

To access further, we can access the int elements within the array elements. For example: B[0][0] accesses the first element of the first element.

Thus, we can see that the array forms a two-dimensional matrix, and we can easily access each of its elements using indices.

Concept and Usage of Multidimensional Arrays in C Language2.1 Initialization of Two-Dimensional ArraysConcept and Usage of Multidimensional Arrays in C Language

To initialize a one-dimensional array:

int A[10] = {0, 1, 2, 3, 4, 5,6,7,8,9};

For a two-dimensional array, each of its elements is an array.

int B[5][10] = {

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, {20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, {30, 31, 32, 33, 34, 35, 36, 37, 38, 39}, {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}

}

Additionally, we can omit the inner curly braces.

int B[5][10] = {

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

10, 11, 12, 13, 14, 15, 16, 17, 18, 19,

20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30, 31, 32, 33, 34, 35, 36, 37, 38, 39,

40, 41, 42, 43, 44, 45, 46, 47, 48, 49}

When omitting the inner curly braces, if the number of elements is insufficient, subsequent elements will be initialized to 0.

int B[5][10] =

{

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

10, 11, 12, 13, 14, 15, 16, 17, 18, 19

}

2.2 Accessing Elements of a Two-Dimensional ArraySince a two-dimensional array has two indices, we generally use nested loops to traverse the two-dimensional array.Concept and Usage of Multidimensional Arrays in C Language2.3 Modifying Elements of a Two-Dimensional Array

Use the assignment operator to modify elements of a two-dimensional array: B[i][j] = B[i][j] * 2; // Modify the values of the two-dimensional array

The following code sets each element of the two-dimensional array to be 2 times its original value.Concept and Usage of Multidimensional Arrays in C Language3. Higher-Dimensional Arrays

If we use a two-dimensional array as an element of an array, we can achieve a three-dimensional array. Similarly, we can create arrays of even higher dimensions.

int C[2][5][10];

The array C has two elements, each of which is a two-dimensional array of type int[5][10]. For a three-dimensional array, we can use triple loops to access or modify the element values.

Concept and Usage of Multidimensional Arrays in C Language3.1 Modifying Elements of a Three-Dimensional ArrayUse the assignment operator to modify elements of a three-dimensional array: B[i][j][k] = B[i][j][k] * 2; // Modify the values of the three-dimensional arrayThe following code sets each element of the three-dimensional array to be 2 times its original value.Concept and Usage of Multidimensional Arrays in C Language_Dokiii’s Thoughts:In my busy schedule, I took some time to revisit my knowledge of C language. I am just a small learner, always eager to learn from the experts. I welcome any constructive criticism and hope to exchange ideas for mutual growth (I am truly grateful). However, please refrain from being rude; everyone is busy with work, and after a long day, I just want to jot down some content. If there are genuine issues, I am open to reasonable feedback and corrections, but if you are rude, who knows 🤷 I might respond back. The world is vast, and there are all kinds of people 🙃 (I still want to say, dogs are everywhere…)

Leave a Comment