c/c++语言开发共享C++程序设计入门 之常量学习

常量: 常量的定义格式:const datatype CONSTANTNAME = VALUE 常量的命名规范:符号常量(包括枚举值)必须全部大写并用下划线分隔单词 例如:MAX_ITERATIONS, COLOR_RED, PI 常量与指针: two features of a pointer(指 …

常量:

常量的定义格式:const datatype constantname = value

常量的命名规范:符号常量(包括枚举值)必须全部大写并用下划线分隔单词 例如:max_iterations, color_red, pi

常量与指针:

two features of a pointer(指针的两个属性):

 pointer variable (指针变量本身)

 data that the pointer points to (指针变量所指向的数据) 

常量和指针的组合:

1.常量指针/常指针:

特征:指针所指向的内容不可以通过指针的间接引用(*p)来改变。

const int* p1; const int x = 1;   p1 = &x;      // 指针p1 的 类型是(const int*)  *p1 = 10;     // error!

2.指针常量:

 特征:指针本身的内容是个常量,不可以改变。

int x = 1, y = 1;   int* const p2 = &x; // 常量p2 的 类型是(int*)  *p2 = 10;     // okay! x=10  p2 = &y; // error! p2 is a constant

 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/604363.html

(0)
上一篇 2021年5月12日
下一篇 2021年5月12日

精彩推荐