const

作者: SayFarewell123 | 来源:发表于2017-07-09 10:58 被阅读0次

const 修饰 一般变量 或者 指针变量
int const a = 1;
int const *b;

被const 修饰的 右边部分为常量,不能变
int b = 3;
int *a = &b;

*a = 8      // 可以


int b = 3;    
    int const *a = &b; 

*a = 8      // 不可以
a = &c      // 可以

例子:

int * const p1; // p1: 只读, *p1: 变量
int const *p2; // p2: 变量 *p2: 只读
const int *p3; // p3: 变量 *p3: 只读
const int * const p4; // p4: 只读 *p4:只读
int const * const p5; // p5: 只读 *p5:只读

相关文章

网友评论

      本文标题:const

      本文链接:https://www.haomeiwen.com/subject/dbeshxtx.html