6.10 Pointers and const

原完整教程链接:6.10 Pointers and const

1.int*指针不能指向const int。
2.const int*指针可以指向int。

3.The following is okay:

int value = 5;
const int *ptr = &value; // ptr points to a "const int"
value = 6; // the value is non-const when accessed through a non-const identifier

But the following is not:

int value = 5;
const int *ptr = &value; // ptr points to a "const int"
*ptr = 6; // ptr treats its value as const, so changing the value through ptr is not legal

4.We can also make a pointer itself constant. A const pointer is a pointer whose value can not be changed after initialization.
To declare a const pointer, use the const keyword between the asterisk and the pointer name:

int value = 5;
int *const ptr = &value;

5.It is possible to declare a const pointer to a const value by using the const keyword both before the type and before the variable name:

int value = 5;
const int *const ptr = &value;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容