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;
推荐阅读
-
6.10 Pointers and const
-
Leetcode 116. Populating Next Right Pointers in Each Node(容易理解的解法)
-
NOTES - Array of Pointers to Strings / 指向str array的多级指针数组
-
two pointers 双针法
-
4.6 two pointers
-
Move Zeroes——Two Pointers
-
two pointers
-
51nod 1495 中国好区间【2-pointers】
-
leetcode: 2 pointers
-
2021暑假Leetcode刷题——Two Pointers(2)