C language array (on)

How to create an array

    //创建一个数组-存放整型-10个
    int arr[10] = { 1, 2, 3 };

In this way, we have created a one-dimensional array,
but remember that for array creation, a constant must be given in [], and variables cannot be used.


Initialization of the
array : Initialization of the array refers to giving some reasonable initial values ​​(initialization) to the contents of the array while creating the array.
E.g:

int arr1[10] = {1,2,3};//这样是不完全的初始化,剩下的元素默认初始化为0

When the array is created, if you don't want to specify a certain size of the array, you have to initialize it. The number of elements in the array is determined according to the initialized content. But for the following code to distinguish, how to allocate the memory.

int main()
{
    char arr4[] = "abcdef";
    printf("%d\n", sizeof(arr4));
    //sizeof 计算 arr4所占空间的大小
    //7个元素-char 7*1 = 7
    printf("%d\n", strlen(arr4));
    //strlen 求字符串的长度 -'\0'之前的字符个数
    //[abcdef\0]
    //6
    return 0;
}

Here we need to know the difference between strlen and sizeof 1. strlen and sizeof
have nothing to do with
2. strlen is to find the length of the string-only the length of the string-library function-use the reference header file
3. sizeof calculation variables, The size of the array and type-the unit is byte-operator


One-dimensional arrays use the
[] subscript reference operator, which is actually an operator for array access.


Summary
1. Arrays are accessed using subscripts, which start from 0.
2. The size of the array can be calculated.


Storage of one-dimensional arrays in memory

int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        printf("&arr[%d] = %p\n",i, &arr[i]);
    }
}

C language array (on)
The above is the output result. From arr[0] to arr[9], it can be seen that as the array index increases, the address of the element also increases regularly. It can be concluded that the array is stored continuously in memory.


Next is the creation of a two-dimensional array

int arr[3][4];
char arr[3][5];
float arr[2][4];

Initialization of two-dimensional array

int arr[3][4] = {1,2,3,4};
int arr[3][4] = {{1,2},{3,4}};
int arr[][4] = {{1,2},{3,4}};

At this time, the sharp-eyed friends can find that there is no number in the [] in the previous representative row of the third two-dimensional array, because in the two-dimensional array, the initialization row of the two-dimensional array can be omitted but the column cannot be omitted . (Supplement: int arr[3][4] represents the creation of three rows and four columns)


The use of
two -dimensional arrays The use of two-dimensional arrays is also through subscripts.


Storage of two-dimensional array in memory

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

Print result: From the
C language array (on)
result, we can see that, like a one-dimensional array, a two-dimensional array is continuously stored in the memory .

2021.1.26
Always believe in fool computers

Guess you like

Origin blog.51cto.com/15080720/2607578