不允许在类型"const int**const"和"int**"之间初始化,为什么?

Rob*_*ves 2 c pointers casting xlc zos

使用V1.8 z/OS XL C编译器,使用INFO(ALL)警告升级,我在下面的代码第4行收到以下警告:

WARNING CCN3196 Initialization between types "const int** const" and "int**" 
                is not allowed.


1  int foo = 0;
2  int *ptr = &foo;

3  const int * const fixed_readonly_ptr = ptr;

4  const int ** const fixed_ptr_to_readonly_ptr = &ptr;
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么我会收到这个警告.如果我可以将一个int指针指向一个指向const int(第3行)的const指针,那么为什么我不能将一个int指针的地址分配给指向const int指针的const指针?我错过了什么?

请注意,上面的代码是一个精简的示例,只是显示了我在少量代码中遇到的问题.真正的上下文是我有一个const指针指向struct(struct s**const),并将它作为参数传递给函数,该函数的参数被定义为指向const结构的指针的const指针(const struct s**)常量).这是因为函数不会修改结构中的数据(因此是第一个const),并且它不会修改始终保存传入的地址的指针参数(因此第二个const).指向的指针的值可以通过这种方式改变(这就是**之间没有第三个const的原因).

Pav*_*aev 5

这是一种类型安全违规.考虑一下这段代码(我const稍微改了一下,以明确它是否适用于指针或指针,但语义上它意味着完全相同的事情):

int* p = 0;
int const** pp = &p; // presumably ok

int const c = 123;
*pp = &c; // okay, &c is int const*, and *p is int const* lvalue

*p = 666; // okay, *p is int lvalue

// wait, so we just changed the value of (const) c above, with no const_cast!
Run Code Online (Sandbox Code Playgroud)