Excerpts from C++ Array Knowledge Points

One-dimensional array

The general form of declaring a one-dimensional array is:
<元素类型> <数组名>[<元素个数>];
<element type> specifies the type of the array element, which can be a simple data type such as integer, real, boolean, and character, or a user-defined composite data type, including arrays Type;
<array name> is represented by the identifier, which is the name of the entire array; the square brackets after <array name> are required. C and C++ treat [] as an operator with the same precedence as the brackets, left associative;
The <number of elements> in square brackets is an integer constant expression, which can be omitted, indicating the number of array elements. Note that the subscripts of the array elements are counted from zero.

E.g:

int a[10];

The above declares an integer array named a, which has 10 integer elements, and the subscript varies from 0 to 9.

Access to array elements

Given an array, we can access the elements in the array in the following form:

<数组名>[<下标表达式>]

<subscript expression> is an expression whose value is an integer, which indicates the subscript of the array element to be accessed.
For example, the 10 elements of the array a can be represented as: a[0], a[1], a[2],..., a[9].
Note: If the subscript used when accessing array elements exceeds the number of elements, it will cause an out-of-bounds access error. C and C++ do not check for out-of-bounds access. If an out-of-bounds access occurs, it may cause errors in the program at runtime.
The elements of the array are sequentially stored in a continuous memory cell. The memory allocation of the array int a[10] is shown in the figure below:

Insert picture description here

Initialization of the array

The method of array initialization is to put the initial values ​​in curly braces in order, and separate the values ​​with commas.
E.g:

int  a[10]={
    
    4};
float  r[20]={
    
    0.1, 5.1};
double  d[3]={
    
    10.0,5.0,1.0};

Need to pay attention to the following two points:
1. If the number of initial values ​​is less than the number of array elements, the remaining elements of the array are automatically initialized to 0.
E.g:

int  n[5]={
    
    0};  //将数组 n 的所有数组元素都初始化为 0

Note: The number of initial values ​​cannot exceed the number of elements in the array.
2. The number of array elements can be omitted when declaring the array. At this time, the system will determine the number of array elements according to the number of initial values.
E.g:

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

In fact, an array x containing 5 integer elements is declared: x[0] …… x[4], the values ​​of which are initialized to 1, ……, 5 respectively.
Here is another point: we can't directly manipulate an array, we can only manipulate a certain element in the array. In other words, the array needs to be "disassembled for use", and only one array element can be operated at a time. The method of manipulating array elements is the same as that of ordinary variables. The method of "dismantling the array" is to access the array by adding a subscript to the array name Element way.
E.g:

a[7]= 4;     // 给数组元素 a[7] 赋值
x = 2 * a[i+2];     // 数组元素作表达式的运算量

Array traversal

For arrays, the most common usage is to use loop statements to manipulate the array and traverse and process the elements of the array.
For example, the following program sums the elements in the array n:

int n[10] = {
    
    12,34,55,71,1,65,423,19,540,10};     // 声明数组n并初始化
int i, sum = 0;     // 定义循环变量和累加和变量
// 循环遍历数组并求和
for (i = 0; i <= 10-1; i++)
{
    
    
    sum += n[i];
}
cout << "The summary is: " << sum << endl;     // 输出求和结果

Array as a function parameter

Since the storage of the array is a continuous unit in the memory, the number of array elements is also uncertain (different arrays may contain different numbers of array elements), so the function to pass the array needs to pass two pieces of information, one is the address of the array (The array name is the array address), tell the called function where the array is stored in the memory, and the other is the number of array elements, tell the called function the allowed subscript range when accessing the array (to prevent access out of bounds).
The function prototype of the function whose parameter is passed in an integer array is as follows:

void  convertScores(int a[],  int len);

The first parameter a is a formal parameter of an array type. The parameter a receives the array name of the incoming array, which is the first address of the array. The length of the array in the square brackets after it is not required. If there is, the compiler will ignore it; the
second parameter len is an integer parameter . The parameter len is passed in the length of the array a, that is, a has len array elements, and the index range for accessing the elements of a is 0 to len-1.
For example: the following program converts the percentile scores of 10 students stored in an integer array into a 5-point system and outputs it.

#include <iostream>
using namespace std;
// 分数转换函数的函数原型,其中:s-学生成绩数组名,len-学生成绩个数
void convertScores(int s[], int len);
int main()
{
    
    
    // 定义学生成绩数组并初始化
    int scores[10] = {
    
    85, 63, 72, 52, 95, 82, 77, 69, 88, 73};
    convertScores(scores, 10);     // 调用分数转换函数convertScores
    // 转换完成后输出分数数组中的分数
    for(int i = 0; i < 10; i++)
        cout << "scores["<<i<<"] = " << scores[i] << endl;
    return 0;
}
// 函数convertScores
void convertScores(int s[], int len)
{
    
    
    // 逐个访问数组中的每个元素,并做转换
    for (int i = 0; i < len; i++)
        s[i] = s[i]/20;     // 修改 s[i] 实际上就是修改 main 函数中的 scores[i],因为 s 和 scores 拥有相同的地址值
}

The output of the program is:

scores[0] = 4
scores[1] = 3
scores[2] = 3
scores[3] = 2
scores[4] = 4
scores[5] = 4
scores[6] = 3
scores[7] = 3
scores[8] = 4
scores[9] = 3

In the above example, the function call convertScores(scores, 10); causes every element in the array scores to be converted. In other words, the modification operation performed on the elements of the array s (formal parameters) in the body of the called function actually acts on the elements of the array scores (real parameters). Is this just passing by reference?
In fact, C and C++ strictly adopt the value-passing method when dealing with array parameters.
There are special places in the way of parameter passing arrays. To understand the mechanism of passing arrays, you must first clarify the following two points:
Like general variables, the array name also has a value, and the value of the array name is the address of the first element of the array. (First address);
Like general variables, when an array is passed as a function parameter, the value of the array variable is also passed, that is, the address of the first element of the array.
Because the value of the array name is the address of the first element of the array, and the way to access the array element is the array name plus the subscript (that is, the first address plus the subscript), so that although the value-passing method is adopted, it is In the function convertScores, the array elements are accessed by adding subscripts to s. In fact, what is accessed is the array elements of the array scores defined in the main function, because s and scores represent the same address location.

Guess you like

Origin blog.csdn.net/interestingddd/article/details/114022912
Recommended