类型“const int** const”之间的初始化和“int**”不允许,为什么?

发布于 2024-08-05 09:22:41 字数 775 浏览 7 评论 0原文

使用 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;

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

请注意,上面的代码是一个简化的示例,仅显示我在少量代码中遇到的问题。真正的上下文是,我有一个指向 struct 的指针 (struct s** const) 的 const 指针,并将其作为参数传递给一个函数,该函数的参数被定义为指向 const struct 的指针的 const 指针 (const struct s**常量)。这是因为该函数不会修改结构中的数据(因此是第一个 const),并且不会修改始终保存传入地址的指针参数(因此是第二个 const)。顺便说一下,所指向的指针的值可能会改变(这就是为什么 ** 之间没有第三个 const 的原因)。

Using V1.8 z/OS XL C compiler, with warnings jacked-up using INFO(ALL), I get the following warning on line 4 of the code below:

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;

I can't wrap my head around why I'm getting this warning. If I can assign an int pointer to a const pointer to const int (line 3), then why can't I assign the address of an int pointer to a const pointer to pointer to const int? What am I missing?

Note the code above is a slimmed down example just showing the issue I'm encountering in a small amount of code. The real context is that I have a const pointer to pointer to struct (struct s** const) and am passing it as an argument to a function who's parameter is defined as a const pointer to pointer to const struct (const struct s** const). This is because the function will not modify the data in the struct (hence the first const) and it does not modify the pointer parameter which always holds the address passed in (hence the second const). The value of the pointer pointed to may be changed by the way (which is why there is NOT a third const in between the **).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单挑你×的.吻 2024-08-12 09:22:41

这是类型安全违规。考虑一下这段代码(我稍微调整了一下 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!

It's a type safety violation. Consider this code (I shuffled const around a bit to make it clear whether it applies to pointer or pointee, but semantically it means exact same thing):

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!
痴梦一场 2024-08-12 09:22:41

C 规则是,您可以将指向某些内容的指针转换为指向 const 某些内容的指针,但该内容必须是完全相同的类型,包括链下游的 const 和 volatile 限定。

该规则的基本原理是,如果允许这两行中的第二行:

int *ptr;

const int ** const fixed_ptr_to_readonly_ptr = &ptr;

那么这可以用于在不进行强制转换的情况下破坏类型安全。

const int i = 4;

// OK, both sides have type const int *
*fixed_ptr_to_readonly_ptr = &i;

// the value of fixed_ptr_to_readonly_ptr is still &ptr
// the value of ptr is now &i;

*ptr = 5;

// oops, attempt to change the value of i which is const

The C rule is that you can convert a pointer to something to a pointer to const something, but that something has to be exactly the same type including const and volatile qualifications further down the chain.

The rationale for this rule is that if the second of these two lines were allowed:

int *ptr;

const int ** const fixed_ptr_to_readonly_ptr = &ptr;

then this can be used to break type safety without a cast.

const int i = 4;

// OK, both sides have type const int *
*fixed_ptr_to_readonly_ptr = &i;

// the value of fixed_ptr_to_readonly_ptr is still &ptr
// the value of ptr is now &i;

*ptr = 5;

// oops, attempt to change the value of i which is const
染墨丶若流云 2024-08-12 09:22:41

这是类型安全违规。您可能想使用 const int * const * 代替。
请参阅 http://www.parashift.com/c++ -faq-lite/const- Correctness.html#faq-18.17

This is a type-safety violation. You probably want to use a const int * const * instead.
See http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文