C language - multidimensional array

Knowledge point 1: Arrays as elements of arrays

          1) There is an array A, whose elements are of type int, and the number of elements is 10; there is also an array B, whose element type is an array containing 10 int elements , and the number of elements is 5;

            2) The declaration of the array consists of the array name , element type , and number of elements ;

                 a) Array name: B

                 b) Element type: int[10]

                 c) Number of elements: 5

int[10] B[5];

@@ The square brackets on the left side of the array name are moved to the far right.

int B[5][10];

@@ This is the declaration of array B. Array B contains 5 elements, and each element is an array containing 10 int elements;

 Knowledge point two: two-dimensional array

            1) Two-dimensional array initialization; element type  array name [ number of elements ] = { comma-separated element content }

                a) We initialize a one-dimensional array like this;

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

                b) For a two-dimensional array, each element 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}
}

               c) Similar to a one-dimensional array, if the number of constants in the initialization list is less than the number of elements, the elements will be initialized with 0;

int B[5][10] = {
{0, 1, 2, 3, 4, 5},
{10, 11, 12, 13, 14, 15},
{20, 21, 22},
{30},
{}
}

                d) You can also omit the curly braces inside;

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
}

                 e) When the inner curly braces are omitted, if the number of elements is insufficient. Then subsequent elements will be initialized with 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) Access or modify two-dimensional array elements

                 a) Access two-dimensional array elements (since two-dimensional arrays have two subscripts, we generally use nested loops to traverse two-dimensional arrays);

#include <stdio.h>
int main()
{
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}
};
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 10; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}

               

                 b) Modify the two-dimensional array elements (you can use the assignment operator to modify the two-dimensional array elements);

B[i][j] = B[i][j] * 2; // 修改二维数组值

       @The following code sets each element of the two-dimensional array to 2 times its original size

#include <stdio.h>
int main()
{
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}
};
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 10; j++)
{
B[i][j] = B[i][j] * 2; // 修改二维数组值
}
}
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 10; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}

 Knowledge point 3: Higher-dimensional arrays

            1) If a two-dimensional array is used as an element of the array, a three-digit array can be implemented. By analogy, higher-dimensional arrays can be implemented;

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

@@Array C has two elements, each element is a two-dimensional array of type int[5][10].

           2) For three-dimensional arrays, we will use triple loops to access or modify element values;

#include <stdio.h>
int main()
{
int B[2][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}
},
{
{-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}
}
};
for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 5; j++)
    {
        for(int k = 0; k < 10; k++)
        {
            printf("%d ", B[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}

 

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/127482767