Chapter 5: The relationship between arrays and pointers in C++

Chapter 5: The relationship between arrays and pointers in C++

The relationship between array and pointer in C++

In C++, there is a close relationship between arrays and pointers. The array name itself is a pointer to the first element of the array, and the pointer can be used to access and manipulate array elements. This article will deeply explore the relationship between arrays and pointers in C++, including the conversion between pointers and one-dimensional arrays and two-dimensional arrays, and combine rich code examples and practical cases to help you better understand this relationship.

Arrays and pointers

In C++, an array name represents a pointer to the first element of the array. Use pointers to access array elements or perform other operations.

Here is a simple example that demonstrates how pointers interact with one-dimensional arrays:

int numbers[5] = {
    
    2, 4, 6, 8, 10};
int* ptr = numbers;

cout << "第一个元素:" << *ptr << endl; // 输出第一个元素:2
cout << "第三个元素:" << *(ptr + 2) << endl; // 输出第三个元素:6

In the above code, we created an array of integers numbersand initialized it. numbersThen, a pointer to the first element of the array is declared ptr. Through *ptrwe can get the value of the element pointed to by the pointer, through which *(ptr + 2)we can get the value of offset 2 of the element pointed to by the pointer.

We can also use pointers to iterate over the entire array:

int* ptr = numbers;
for (int i = 0; i < 5; i++) {
    
    
    cout << *ptr << " ";
    ptr++;
}

In the above code, we ptrinitialize the pointer to numbersthe address of the first element of the array, and then ptrtraverse the entire array by incrementing the pointer and outputting the value pointed to by the pointer.

Conversion between pointers and one-dimensional arrays

In C++, an array name can be interpreted as a pointer to the first element of the array. This feature allows us to manipulate array contents using pointers and vice versa.

Here is an example showing the conversion between arrays and pointers:

int numbers[] = {
    
    2, 4, 6, 8, 10};
int* ptr = numbers;

cout << ptr[0] << endl; // 输出第一个元素:2
cout << numbers[1] << endl; // 输出第二个元素:4

ptr[2] = 12; // 修改第三个元素的值为12
cout << numbers[2] << endl; // 输出修改后的值:12

In the above code, we declared an array of integers numbersand initialized it. Then, we declare a pointer ptrand set it as numbersthe address of the first element of the array. Next, we pass ptr[0]and numbers[1]access the same element. Finally, by ptr[2] = 12modifying the value of the third element, and by numbers[2]verifying the modified result.

Conversion between pointers and two-dimensional arrays

In C++, a two-dimensional array is actually a one-dimensional array in contiguous memory space. Through pointer arithmetic, we can treat two-dimensional arrays as one-dimensional arrays for access and manipulation.

Here is an example showing how pointers interact with two-dimensional arrays:

int matrix[3][4] = {
    
    
    {
    
    1, 2, 3, 4},
    {
    
    5, 6, 7, 8},
    {
    
    9, 10, 11, 12}
};

int* ptr = &matrix[0][0];

cout << ptr[0] << endl; // 输出第一个元素:1
cout << *(ptr + 5) << endl; // 输出第二行第二个元素:6

In the above code, we declare a two-dimensional array of integers matrixand initialize it. We then use a pointer ptrto set it to matrixthe address of the first element of the array. By ptr[0]and *(ptr + 5)we can access the same elements.

actual case

Let's look at a more complex example, combining pointer and two-dimensional array conversion problems:

const int ROWS = 3;
const int COLS = 4;

int matrix[ROWS][COLS];

// 使用指针遍历和填充二维数组
int* ptr = &matrix[0][0];
for (int i = 0; i < ROWS * COLS; i++) {
    
    
    *ptr = i;
    ptr++;
}

// 使用指针遍历和输出二维数组
ptr = &matrix[0][0];
for (int i = 0; i < ROWS; i++) {
    
    
    for (int j = 0; j < COLS; j++) {
    
    
        cout << *ptr << " ";
        ptr++;
    }
    cout << endl;
}

In the above code, we matrixinitialized the pointer with the address of the first element of the array ptr. Then, in a loop, we use pointers to fill the entire 2D array. Next, use the pointer again to traverse and output the entire two-dimensional array from beginning to end.

The output is as follows:

0 1 2 3 
4 5 6 7 
8 9 10 11

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132241211