指针和const

来源:互联网 发布:知乎宇宙高维 编辑:程序博客网 时间:2024/06/15 09:43
const int * pt = &age;*pt += 1;//invalid because pt points to a const int

*pt 的值为const int ,不能被修改。可以通过age来修改age的值,但pt不可以。

int sage = 80;const int * pt = &sage;

可以将一个新的地址赋给pt。

不能将const的地址赋给常规指针。

int * const finger = &sloth;

finger只能指向sloth。finger 和 *ps 都是const ,而*finger和 ps不是。

原创粉丝点击