顶层const和底层const

1.顶层const表示指针本身是一个常量。

  int i = 0;

  int *const p1 = &i;

  const int ci = 42;

代表指针指向的地址是不能变的。

 

2.底层const表示指针指向的对象是一个常量。

  const int ci = 2; 

  const  int *p2 = &ci;

p2的值可以改变,即可以指向别的对象,但是指向的对象必须是常量。

posted @ 2019-08-08 17:32  penuel  阅读(502)  评论(0编辑  收藏  举报