Initialization of C++ one-dimensional array and possible problems

The definition form of a one-dimensional array is:

Data type Array name [constant expression], where the constant expression is used to indicate the size of the array (the number of elements in the array). The definition of an array in C++ is: a structural data type arranged in a certain order by a certain number of elements of the same data type. Therefore, the size of the array must be determined, and only constant expressions can be used to represent the size of the array, and if the size of the array is represented by variables (it is very likely to do this when you are a beginner), for example:

int a = 5;
int num[a];

An error will be reported as soon as it is run:

It would work if rewritten like this:

const int a = 5;
int num[a];

The initialization method of a one-dimensional array:

1. Initialize the assignment while defining the array, and use curly braces to list the values ​​​​to be assigned in order. At this time, the overall assignment of the array is performed, and the specific form is as follows:

int num[5] = { 1,2,3,4,5 };
for (int i = 0; i < 5; i++) {
	cout << num[i] << ' ';
}

At this time, each element of the array num is assigned a corresponding value:

num[0] num[1] num[2] num[3] num[4]
1 2 3 4 5

Actual operation:

 

When the array is defined, the overall assignment does not need to assign values ​​to all elements, but only a part of the elements can be assigned, but this part of the elements can only be the consecutive elements in the previous part of the array, which is the default in C++. Because the computer assigns values ​​to the elements one by one in order when assigning values ​​to the overall array, if the default is not the case, for example, instead of assigning values ​​to the first element of the array, but assigning values ​​to the second and third elements, the user will have to enter two number (assuming it is possible); if only assigning values ​​to the first and second elements, the user also needs to input two numbers, then for the same two inputs (the assignment targets are different), the computer cannot know the user provided Whether the first number of the array is assigned to the first element of the array or the second element is assigned, so there is ambiguity and an error will occur.

When an array is defined, some elements are assigned as follows:

int num[5] = { 1,2,3 };
for (int i = 0; i < 5; i++) {
	cout << num[i] << ' ';
}

At this time, the value of each element of the array num:

num[0] num[1] num[2] num[3] num[4]
1 2 3 0 0

The actual situation:

It can be seen that if the elements in the array are not initialized and assigned, C++ will assign a value of 0 by default.

When assigning values ​​to all elements, it is not necessary to specify the size of the array, which increases some flexibility. The specific form is as follows:

int num[] = { 1,2,3,4,5 };
for (int i = 0; i < 5; i++) {
	cout << num[i] << ' ';
}

operation result:

 After the array with no defined size is assigned as a whole (5 numbers), the size of the array is determined to be 5.

2. After the array is defined, assign the value by pressing the mark. The specific form is as follows:

int num[5];
for (int i = 0; i < 5; i++) {
	num[i] = i + 1;
}

The value of each element of the array at this time:

num[0] num[1] num[2] num[3] num[4]
1 2 3 4 5

Of course, you can also input from the keyboard, press the mark to assign values ​​to the array elements, the form is as follows:

int num[5];
for (int i = 0; i < 5; i++) {
	cin >> num[i];
}

3. Use the pointer property of the array name to assign values ​​to the array elements.

An array name is actually a pointer to the first element of the array. Since the storage addresses of each element of the array are consecutive in order, the pointer property of the array name can be used to assign values ​​to the array elements. The specific form is as follows:

int num[5];
for (int i = 0; i < 5; i++) {
	*(num + i) = i + 1;
}

The value of each element of the array at this time:

num[0] num[1] num[2] num[3] num[4]
1 2 3 4 5

Actual operation:

 

Errors that may occur when initializing a one-dimensional array:

The array is not initialized when it is defined, and the overall curly braces of the array are assigned after definition, for example:

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

This is not advisable , and the error is as follows:

 Therefore, you cannot use curly braces to assign values ​​after the array definition.

When using array subscripts to assign values, it is also necessary to avoid the occurrence of array subscripts out of bounds (although it rarely happens, but it should also be noted that when the code becomes more varied and complex, it is very likely to happen if you don’t pay attention).

Guess you like

Origin blog.csdn.net/XIETINGYUWO/article/details/127042514