gpt4 book ai didi

c++ - 我们在哪里以及为什么使用 "pointers that point to constants"、 "constant pointers"和 "constant pointers that point to constants"?

转载 作者:行者123 更新时间:2023-11-28 06:37:41 24 4
gpt4 key购买 nike

所以如果我在 C++ 中有这样的东西:

char A_char = 'A';
char * myPtr = &A_char;

const char * myPtr = &char_A; //pointers that point to constants
char * const myPtr = &char_A; //constant pointers
const char * const myPtr = &char_A; //constant pointers that point to constants

我想知道在编程中我们在哪里以及为什么使用“指向常量的指针”、“常量指针”和“指向常量的常量指针”。我知道它们之间的区别和它们的语法,但我不知道我们在哪里以及为什么使用它们。你能解释一下吗?谢谢大家。

最佳答案

常量指针是指不能改变其所持有地址的指针。

<type of pointer> * const <name of pointer>

无法更改其指向的变量值的指针称为指向常量的指针。

const <type of pointer>* <name of pointer>

指向常量的常量指针是既不能改变其指向的地址也不能改变保存在该地址的值的指针。

const <type of pointer>* const <name of pointer>

您可以在此处找到有关其用法的更多详细信息: http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967/Constant-Pointers-and-Pointers-to-Constants.htm

关于c++ - 我们在哪里以及为什么使用 "pointers that point to constants"、 "constant pointers"和 "constant pointers that point to constants"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26528406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com