gpt4 book ai didi

c - int** 和 const int** 可以别名吗?

转载 作者:太空狗 更新时间:2023-10-29 17:03:28 25 4
gpt4 key购买 nike

据我了解,这样的事情是可以的:

const int ci = 42;
const int *cip = &ci;
int *ip = (int *)cip;
int j = *ip;

这个呢?

const int ci = 42;
const int *cip = &ci;
const int **cipp = &cip;
int **ipp = (int **)cipp;
int j = **ipp;

最佳答案

表达式 *ippint * 类型的左值,但是它被用来访问有效类型 const int * 的对象>。 (即 cip)。

根据标准的规定,这是一个严格的别名违规:允许别名的类型列表不包括别名 T * as const T * 或反之亦然。

最接近的异常(exception)是这个:(C11 6.5/6 摘录)

  • a qualified version of a type compatible with the effective type of the object

C11 6.2.5/26明确定义了“合格版本”:

Each unqualified type has several qualified versions of its type, corresponding to the combinations of one, two, or all three of the const, volatile, and restrict qualifiers. The qualified or unqualified versions of a type are distinct types that belong to the same type category and have the same representation and alignment requirements. A derived type is not qualified by the qualifiers (if any) of the type from which it is derived.

因此异常(exception)情况是 T 可能被别名为 const T 反之亦然,但是对于可别名类型的指针没有类似的异常(exception)情况。 const T * 不是 T *合格版本


当然还有脚注:

The intent of this list is to specify those circumstances in which an object may or may not be aliased

我不能说这条规则的目的是让 const T *T * 是否可以别名。我似乎不清楚指定 T *const T * 具有“相同的表示和对齐要求”(6.2.5/28)的目的是什么它不可别名。

关于c - int** 和 const int** 可以别名吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45124063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com