当前位置:网站首页>6. const usage, combination of first and second level pointers

6. const usage, combination of first and second level pointers

2022-06-23 01:40:00 For financial freedom!

 Insert picture description here
C file :
 Insert picture description here
It's all printed out 30,const The amount of modification is called a constant ! Cannot be an array parameter !

C++ file :
 Insert picture description here
C++ in , All appear const Where constant names are , Are replaced by the initialization of constants !( Refer to detai C++ Anatomy column , It involves the concept of constant table !) Design to constants at compile time a Is replaced by a constant !

const Combined with one or two level pointers
sonst The common mistake in the amount of decoration is :

  1. Constants can no longer be used as lvalues
  2. The address of a constant cannot be disclosed to an ordinary pointer or ordinary reference variable ( You can modify the value of a constant indirectly !)

C++ language norm :const It's the type closest to him !
 Insert picture description here
first line :*p Cannot be used as an lvalue :*p=20 error ,p=&b Sure (const Modification is int type )
The second line : Same as the first line , Modification is int
The third line : Modification is int * , in other words p Is a constant , Cannot point to other memory !*p Assignable ( Modify the value of the memory pointed to through pointer dereference )
In the fourth row : first const modification int(*p), The second modification int*(p)!

const Sum up the formula :
const If there is no pointer on the right * Words ,const It's a non participating type !
 Insert picture description here
The above is wrong , Will report a mistake !
 Insert picture description here

原网站

版权声明
本文为[For financial freedom!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514414059.html