In-depth understanding of pointers 2

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

世上有两种耀眼的光芒,一种是正在升起的太阳,一种是正在努力学习编程的你!一个爱学编程的人。各位看官,我衷心的希望这篇博客能对你们有所帮助,同时也希望各位看官能对我的文章给与点评,希望我们能够携手共同促进进步,在编程的道路上越走越远!

想回顾上一节的请点击这里深入理解指针1


提示:以下是本篇文章正文内容,下面案例可供参考

1. Understanding array names

In the previous chapter, when we used pointers to access the contents of an array, we had this code:

int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int *p = &arr[0];

Here we use &arr[0] to get the address of the first element of the array, but in fact the array name is actually the address, and it is the address of the first element of the array. Let's do a test. (The array name is the address of the first element of the array)

#include <stdio.h>
int main()
{
 int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
 printf("&arr[0] = %p\n", &arr[0]);
 printf("arr = %p\n", arr);
 return 0;
}

Output result:

We found that the array name and the address of the first element of the array print exactly the same result.The array name is the address of the first element (the first element) of the array.

Do any students have questions at this time? If the array name is the address of the first element of the array, how do you understand the following code?

The output result is: 40. If arr is the address of the first element of the array, then the output should be 4/8.

In fact, the array name isThe address of the first element (first element) of the array is correct, but there are two exceptions: a>

• sizeof (array name), put the array name separately in sizeof. The array name here represents the entire array, and the size of the entire array is calculated. The unit is bytes.

• &Array name, the array name here represents the entire array, and the address of the entire array is taken out (there is a difference between the address of the entire array and the address of the first element of the array)

In addition, wherever an array name is used, the array name represents the address of the first element.

At this time, some curious students can try this code again:

Here we find that the difference between &arr[0] and &arr[0]+1 is 4 bytes, and the difference between arr and arr+1 is 4 bytes, because both &arr[0] and arr are the first elements. Address, +1 means skipping an element.

But the difference between &arr and &arr+1 is 40 bytes. This is because &arr is the address of the array, and the +1 operation skips the entire array.

At this point everyone should understand the meaning of the array name.

The array name is the address of the first element of the array, with 2 exceptions.

2. Use pointers to access arrays

With the support of the previous knowledge, combined with the characteristics of arrays, we can easily use pointers to access arrays.

Array name = address of the first element of the array
int*p=arr; The address of the first element of the arr array is placed in the pointer variable p, which is equivalent to the address of the first element of the array. Address
So, p is equivalent to the array name p[i]=arr[i], which can be accessed through subscripts to find the corresponding element

arr[i] == *(arr+i) == *(i+arr) == i[arr]

3. The essence of passing parameters through one-dimensional arrays

We have learned about arrays, and as mentioned before, arrays can be passed to functions. In this section we will discuss the essence of passing array parameters. First, let’s start with a question. We used to calculate the number of elements of the array outside the function. Can we pass the function to a function and then find the number of elements of the array inside the function?

We found that the number of elements of the array was not obtained correctly inside the function.

This is about learning the essence of array parameter passing. We learned in the last section:The array name is the address of the first element of the array; then when passing array parameters, pass is the array name, which means that essentiallythe array parameter is passed the address of the first element of the array.

So the function parameter part should theoretically use a pointer variable to receive the address of the first element. Then inside the function we write sizeof(arr) to calculate the size of an address (unit bytes) rather than the size of the array (unit bytes). Precisely because the parameter part of the function is essentially a pointer, there is no way to find the number of array elements inside the function.

Summary:One-dimensional array transfer parameters, the formal parameter part can be written in the form of an array or in the form of a pointer.

4. Secondary pointer

Pointer variables are also variables, and they have addresses.. Where is the address of the pointer variable stored?

Let's take a look at the code demonstration:

The operations for secondary pointers are:

*ppa By dereferencing the address in ppa, what is found is pa , *ppa actually accesses pa .

int b = 20;

*ppa = &b;//Equivalent to pa = &b;

**ppa first finds pa through *ppa, and then dereferences pa: *pa, then what is found is a.

**ppa = 30;

//Equivalent to *pa = 30;

//Equivalent to a = 30;

5. Pointer array

Is an array of pointers a pointer or an array?

Let’s make an analogy.An integer array is an array that stores integers, and a character array is an array that stores characters.

What about the pointer array? is an array storing pointers.

Each element of the pointer array is used to store an address (pointer).

As shown below:

Each element of the pointer array is an address and can point to an area.

Code demo:

6. Pointer array simulates two-dimensional array

Drawing demonstration of parr array

parr[i] accesses the elements of the parr array. The array element found by parr[i] points to an integer one-dimensional array, and parr[i][j] is the element in the integer one-dimensional array.

The above code simulates the effect of a two-dimensional array, but it is not actually a two-dimensional array because each row is not continuous.


Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/134087722