C++ pointer incurable disease

const modified pointer


Constant pointer

const modifies the pointer, the pointer point can be changed, but the value cannot be changed

int a = 100,b=200;

const int * p=&a;	// 指向可以变

*p=100;				// 值是不可以变

Pointer constant

const modified variable, the pointer can not be changed, the value of the pointer can be changed

int * const p=&a;

Decorate pointers, decorate constants

Modifies pointers and constants

int a = 10;
const int * const p = &a; 

const modified pointer (to prevent misoperation)

In many cases we pass a pointer address to a function. In C language, we cannot guarantee the correctness of the data. However, by adding the const keyword in C++, we can guarantee that the address is passed to the function without being modified by the function. value

Guess you like

Origin blog.csdn.net/qq_42418169/article/details/108772513