C/C++ pointers from 0 to 99 (detailed explanation)

Table of contents

1. Basic understanding of pointers

2. Basic use of pointers

3. Why use pointers?

4. The connection between pointers and arrays

5. Expanded use of pointers

1) Array of pointers

2) array pointer

3) Function pointer ·

Structure: return type (*p) (parameter 1, parameter 2);

Example: int (*add)(int a,int b) =add; (add represents the addition function, and the function name is the address of the function)

What is the use of function pointers? Function pointers can simplify a lot of code and make the program more robust. You can think about it. Using a function pointer template can adapt to functions with different functions as long as the return value and parameters are the same, greatly reducing duplicate code.

6) Supplements to sizeof and stelen


1. Basic understanding of pointers

       All data will be stored in the computer. Most of the time we only need part of the data, so how do we find that data accurately? At this time, we need some numbers to find the data, like the numbers on our ID cards, in the computer Each byte has its own number, and pointers are used to store these address numbers.

2. Basic use of pointers

      The structure of the first-level pointer is: (type) + (*) + (name), for example: int * pa; this is a first-level pointer of type int. By dereferencing it, you can get the content in the space pointed by the pointer. The second-level A pointer stores the address of a first-level pointer. For example: int * *paa =&pa; dereferencing it once can obtain the content pointed by the first-level pointer. Dereferencing it once can obtain the content pointed by the first-level pointer, and so on. Can understand level three and level four pointers.

3. Why use pointers?

     First of all, the most common use of pointers is when passing parameters to a function. As we all know, when passing parameters of a data type, most of them are copied. Changes in the function cannot affect the actual parameters in other functions, and because pointers are different from others , the pointer is the essence (address), as long as the address of the actual parameter is passed and the address is found through the pointer of the formal parameter, the operation can avoid the problem of being unable to change due to copying. Secondly, pointers can perform more precise operations on data, and can perform operations on bytes through forced conversion. Pointers can also simplify a large amount of code, and these advantages will be felt when we use them in the future. There are many other advantages that I will not list one by one. If you are interested, you can find information online.

4. The connection between pointers and arrays

        The connection between arrays and pointers is inextricably linked. The essence of an array is a pointer. The name of an array is the address of the first element. Pointers can also be expressed in the form of [ ], and arrays can also be expressed in the form of pointers * (arr +i) expressed in the form. Next, I will show some conversions and correspondences between arrays and pointers.

int *arr              =               int arr[ ]

int (*p)[   ]         =              int  arr[ ] [ ]

5. Expanded use of pointers

1) Array of pointers

Structure: int *p[ ]

The essence of a pointer array is still an array. Its purpose can be used to store pointers for easy use, such as

int *pa=NULL;
int *pb=NULL;
int *pc=NULL;
int *arr[]={pa,pb,pc};

2) array pointer

Structure: int (*p)[ ]

The essence of an array pointer is a pointer. It seems that the structure is the same as that of a pointer array, but don't forget that [ ] and * have different priorities. The p of the array pointer is combined with [] first, while the pointer array is combined with * first. Pointer arrays can store the addresses of multi-digit arrays such as two-dimensional arrays, and can be easily taken out, for example:

3) Function pointer ·

Structure: return type (*p) (parameter 1, parameter 2);

Example: int (*add)(int a,int b) =add; (add represents the addition function, and the function name is the address of the function)

What is the use of function pointers? Function pointers can simplify a lot of code and make the program more robust. You can think about it. Using a function pointer template can adapt to functions with different functions as long as the return value and parameters are the same, greatly reducing duplicate code.

6. Supplements to sizeof and stelen

First of all, we must realize the difference between sizeof and strlen. sizeof only looks at the type in () and does not enter the memory calculation, such as int, and the array is int [10]. And strlen enters the given address memory, finds "\0", and how to count the number of bytes.

If sizeof() contains a pointer, the result is 4||8. Because the compilation environment is different, the size of the pointer is different, and strlen can only contain addresses, and others may access it illegally.

Guess you like

Origin blog.csdn.net/m0_74316391/article/details/131612057