欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

6.10 Pointers and const

程序员文章站 2024-02-29 17:27:16
...

原完整教程链接: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;