C language array one-dimensional array articles

Foreword: This article is mainly used for personal review, the pursuit of simplicity, thank you for your reference, communication and handling, and may continue to be revised and improved in the future.

Because it is a personal review, there will be some compression and omission.

array: a collection of elements of the same type

1. Array

1. Define a one-dimensional array:

int arr[10] = {1,2,3,4,5,6,7,8,9,10};//定义一个整型数组,最多可以放10个元素

Note that the above code is an element, not necessarily an integer element.
eg. 

It can be seen that even if a character is placed, the compiler still does not report an error, and runs normally, printing out the ASCII code value of the character a

2. The value of the array when it is created

When the array is created, if only the size of the array is given and not initialized, the value in it will be a random value

  If so, all values ​​of the array will be initialized to 0. When an array is created and initialized, it infers its size.

#include <stdio.h>
int main()
{
    int arr[10] = { 0 };
    //当然,这样也可以
    int arr[10] = { 0 , 0 };

    //这样是不可以的,不要觉得是0就可以为所欲为
    int arr[5] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

    //如果这样写
    int arr[10] = { 1, 2, 3 };
    //这样是不完全初始化,即数组前三个元素分别初始化为1,2,3,其余元素均默认为0

    return 0;
}

In the following case, when only the value is given without the size of the array, the array will calculate the size by itself

When the array is created, the specification of the size of the array requires a constant, which cannot be a constant in the variable [] (even a value called a constant variable that is modified by const cannot)

PS: The variable modified by const is called a constant variable, which is essentially a variable, but cannot be used to specify the size when the array is defined

Because [] is an operator, it can be written like this, and the two are equivalent (it was possible in some places before, but it doesn't seem to be possible in VS2019, I'll look at it another day)

int arr[10] = { 0 };
int 10[arr] = { 0 };

3. The C language stipulates that each element of the array has a subscript, and the subscript starts from 0.

4. The use of arrays

We can access elements by using the subscript of the element in the array

5. Possible errors when using arrays

 When we accidentally make the wrong subscript of the array, the following situations may occur

Out of bounds access:

One of the classic questions is:

#include <stdio.h>
int main()
{
    int i = 0;
    int arr[10] = {0};
    for(i=0; i<=12; i++)
    {
        arr[i] = 0;
        printf("hello world!\n");
    }
    return 0;
}

 The call to the array itself in this question is out of bounds, but since the i variable is created, it just coincides with the assigned arr[12] space, and the value becomes 0, so the result of the program running in some debug environments is an infinite loop.

The '\0' hidden by the strings in the array affects the array size:

Note that '\0' is hidden in "abcdef". Although the length of arr is 6, the actual number of elements is 7. We can see it through debugging.

 The subscript is 0~6, a total of 7 elements, and the size is 7.

6. Determine whether two array types are the same:

As long as the type of the array changes or the number of elements changes, the overall type changes.

int main()
{
	int arr1[10] = { 0 };
	char arr2[10] = { 0 };
	char arr3[7] = { 0 };


	return 0;
}

All three array types in this code are different.

7. Array parameters

When the array is passed parameters, it is not the entire array, but the address of the first element of the array, so if sz is calculated in the function, there will be a bug.

Guess you like

Origin blog.csdn.net/weixin_60320290/article/details/124009890