当前位置:网站首页>Constants and constant pointers

Constants and constant pointers

2022-07-19 05:30:00 Learn to put down ta

Constant

General constant number , character , character string
When used const After modifying variables, you can change variables into constants

const double pi = 3.14;
pi = 3.1415926;// If you want to change the value pi, May be an error 

pointer to const

It can be seen from the above that : use const A decorated pointer is a pointer to a constant , Its value cannot be modified by dereference .

const int a = 520;
const int *p = &a;
*p = 123;// Will report a mistake 

But the pointer can be modified at this time p The direction of . By contrast , Yes

Constant pointer

The pointer cannot be changed , If it points to a variable, its value can be changed , If it is a constant, it cannot be changed .

int a = 123;
int b = 520;
int * const p = &a;
*p = 111;// After action a The value of is changed to 111
p = &b;// Will report a mistake , Because the pointer is fixed 

Constant pointer to constant and pointer to constant pointer

const int num = 111;
const int * const p = #// Constant pointer to constant 
const int * const *pp = &p;// Pointer to constant pointer to constant 
原网站

版权声明
本文为[Learn to put down ta]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170507599300.html