C language - advanced pointer (1)

Table of contents

​edit

1. Character pointer

1.1 Basic concepts

 1.2 Interview questions

2. Array of pointers

3. Array pointer

3.1 Definition of array pointer

3.2 & array name VS array name

3.3 Use of array pointers

Four. Array parameters, pointer parameters

4.1 One-dimensional array parameter passing

​edit

4.2 Two-dimensional array parameter passing

4.3 First-level pointer parameter passing

4.4 Second-level pointer parameter passing

​edit

Five. Function pointer

6. Ending

1. Character pointer

1.1 Basic concepts

We can print to test whether the string can be used as an array:

The answer is yes, so we have learned a new way of writing, which can directly store constant strings in pointer variables.

 However, there are disadvantages to doing so, that is, it cannot be modified. When we try to dereference p pointing to the first character a (modify the first character to 'e'), an error will occur.

So we can add const in front of it, so that the error will not be changed.

const char* p = "abcdef"

p points to the address, the address cannot be printed with %c, but %s can be used, and since *p is a pointer type of char*, only one character can be obtained after dereferencing, so it must be printed with %c.

 1.2 Interview questions

We can find that s1 and s2 have opened up different spaces to point to their respective addresses. And const is a constant variable, so when there are multiple copies, there is only one by default, so s3 and s4 can only point to the same address together, because it is unique.

Note that the addresses stored in s3 and s4 are the same, but &s3 and &s4 are different, because they are the spaces opened up by themselves to store addresses. The space addresses are different, but the addresses stored in the space (that is, the content) are the same of.

2. Array of pointers

 

 

 Then we can try to access the array through the subscript, and then access the elements in it through the subscript of the accessed array.

 

3. Array pointer

3.1 Definition of array pointer

 

3.2 & array name VS array name

The result is the same, but the first two are the first element address, & arr is not necessarily. Int* is used to fetch the address of arr, and int* is used to fetch &arr[0], because it is essentially to find the elements inside, so what kind of pointer variable should &arr use to receive it?

3.3 Use of array pointers

 Two points need to be clarified: 1 is to determine the type, and 2 is to mark the size.

 We will find it awkward to print like this,

 This is not as convenient as an array of pointers

Four. Array parameters, pointer parameters

 

4.1 One-dimensional array parameter passing

 //arr[i]-->*(arr+i) (the address of the first element + i instead of the entire element address + i)

4.2 Two-dimensional array parameter passing

Two-dimensional array parameter passing requires an array pointer, which points to the address of the entire array, so as to facilitate access to the elements in the next array address.

p[i]-->*(p+i) is actually the array name after dereferencing, and then becomes the array name [j] to access elements.

 

4.3 First-level pointer parameter passing

4.4 Second-level pointer parameter passing

Five. Function pointer

 

 We can find that both the & function name and the function name are the same address.

How to write a function pointer:

 Remember that *pf2 must be enclosed in parentheses, otherwise it will become a function declaration whose return type is Int*.

 The call was successful.

 Share an interesting piece of code:

 

 

 Simplified version:

6. Ending

The advancement of pointers is indeed very convoluted, and this process needs to be continuously analyzed and traced back to the source. Not only do you have to memorize from time to time, but you also need to use pointers more, so that you won’t be stumped when you encounter problems about pointer types in the future.

Guess you like

Origin blog.csdn.net/fax_player/article/details/132520403