【发布时间】:2016-05-25 06:50:18
【问题描述】:
看看这段代码:
int main() {
int foo = 0;
const int *ptr = &foo;
*ptr = 1; // <-- Error here
return 0;
}
编译时,clang给我一个错误:
const.c:5:7: error: read-only variable is not assignable
是的,ptr 是 const,但 foo 不是。为什么我不能为foo 赋值?
【问题讨论】:
-
ptr不是const;*ptr是。 -
int* const ptr = &foo;试试这个。 -
@jrok 这如我所愿,谢谢:)