y*******i 发帖数: 100 | 1 (a) int i = -1;
(b) const int ic = i;
(c) const int *pic = ⁣
(d) int *const cpi = ⁣
(e) const int *const cpic = ⁣
答案说(d)不合法,其他都合法。d怎么不合法了,不是给一个const 的指针赋值了吗
? | g***y 发帖数: 764 | 2 ic is a const int
but cpi is not pointing to a const int
【在 y*******i 的大作中提到】 : (a) int i = -1; : (b) const int ic = i; : (c) const int *pic = ⁣ : (d) int *const cpi = ⁣ : (e) const int *const cpic = ⁣ : 答案说(d)不合法,其他都合法。d怎么不合法了,不是给一个const 的指针赋值了吗 : ?
| y*******i 发帖数: 100 | 3 (e)怎么就合法了,e不但指针是const,还多了一个它指向的对象也要const? | j**w 发帖数: 382 | 4
ic is a constant. that is, the value of ic can not be changed.
(d) int * const cpi. It means, cpi is a constant, and cpi points to an int.
Since cpi is a pointer. "cpi is a constant" just means the cpi points to a "
fixed" address. Say nothing about the content of that fixed address.
If (d) is valid, we can do (*cpi)++. In this case, the value of ic is
changed. It's wrong.
For (e)
const int *const cpic. cpic is a constant, and is pointed to a const int.
Since ic is also a const int. It's good.
Just my 0.02.
【在 y*******i 的大作中提到】 : (a) int i = -1; : (b) const int ic = i; : (c) const int *pic = ⁣ : (d) int *const cpi = ⁣ : (e) const int *const cpic = ⁣ : 答案说(d)不合法,其他都合法。d怎么不合法了,不是给一个const 的指针赋值了吗 : ?
|
|