2
  1. 定义 int 和指向 int 的指针:

    int i = 22, *p = &i;

  2. 定义低级和顶级 const 的指针:

    const int *const cp = p;

    (2) 没问题 - 无权更改 (i) 值的 const 点

  3. 定义一个指向低 + 顶级 const 的指针的指针:

    const int **const cp_2_p = &p;

    (3) 不行,为什么?

    error C2440: 'initializing' : cannot convert from 'int **' to 'const int **const

我希望能够定义一个指针,该指针指向int我不能改变它指向的地址,也不能改变它指向的地址,指向的地址。

4

1 回答 1

7

In general, const applies to the item to its left. The exception of const T is present for historical reasons, and is the conventional alternative to T const.

cp is declared as a constant pointer to a constant int.

cp_2_p is declared as a constant pointer to a non-constant pointer to a constant int.

You would need to declare cp_2_p like this for the types to be compatible:

const int *const *const cp_2_p
于 2013-04-14T07:26:59.913 回答