变量声明问题
解释变量 p 和 q 的声明方式之间的差异。描述何时使用一种声明以及何时使用另一种声明。
int x = 5;
const int *p = &x;
int * const q = &x;
Explain the difference between how variables p and q are declared. Describe when you would use one declaration and when you would use the other.
int x = 5;
const int *p = &x;
int * const q = &x;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将
x
的地址分配给指向const intp
的指针。这意味着p
指向的东西是 const,不能通过取消引用p
来写入。这会将
x
的地址分配给指向int的const指针q
。这意味着,指针是常量,之后无法更改,但是您可以通过取消引用p
来更改x
。This assigns the address of
x
to the pointer to const intp
. That means the thingp
points to is const and cannot be written to by de-referencingp
.This assigns the address of
x
to the const pointer to intq
. That means, the pointer is const and cannot be altered afterwards, you can however alterx
by de-referencingp
.