Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在下面的代码中:
int a=5,b=6,c=7;; int *const ptr = &a; //if ptr = &b; is wrong since it is a pointer constant.
引用指针是否适用?
*ptr = 8; //is it allowed?
如果不是,那为什么?
那么顺从适用于哪些地方呢?
int *const ptr = &a;
在这种情况下,指针是常量,但它指向的数据不是常量。 所以你不能让指针指向其他变量,但你可以修改指向的值。
*ptr = 8;
是允许的,因为它只更改存储在指针指向的地址处的值。