gpt4 book ai didi

c - "signed or unsigned type"在此 C90 未定义行为定义中的含义是什么?

转载 作者:行者123 更新时间:2023-12-02 07:18:12 24 4
gpt4 key购买 nike

在 ANSI C90 标准中,第 6.3 节对表达式有如下说明:

An object shall have its stored value accessed only by an lvalue that has one of the following types: [...] a type that is the signed or unsigned type corresponding to a qualified version of the declared type of the object

附件 G.2 中有这个未定义行为的实例:

The behavior in the following circumstances is undefined: [...] An object has its stored value accessed by an lvalue that does not have one of the following types: the declared type of the object, a qualified version of the declared type of the object, the signed or unsigned type corresponding to the declared type of the object, the signed or unsigned type corresponding to a qualified version of the declared type of the object, an aggregate or union type that (recursively) includes one of the aforementioned types among its members, or a character type (6.3).

我发现强调部分的措辞含糊不清,很难解释。

  1. 是否表示“有符号则为原类型对应的有符号类型,无符号则为原类型对应的无符号类型”;或“原始类型对应的类型(有符号或无符号无关紧要)”?也就是说,是:

    signed int a = -10;
    unsigned int b = *((unsigned int *) a);

    ...未定义?

  2. 如果有符号/无符号无关紧要,因为标准区分了三种类型 charsigned charunsigned char,是否会定义通过 signed char *unsigned char * 访问 char

最佳答案

它是说将值转换为不同的符号不是不是未定义的行为。如果对象声明为 signed int,您可以使用 unsigned int 左值访问它,反之亦然。

当它说“对象的声明类型”时,已经涵盖了签名相同的情况,尽管这种情况也可以被认为是这样说。

char 的情况下,signed charunsigned char 都是“对应于”该类型的“有符号或无符号类型”。

总而言之,这只是说左值的符号性不会影响访问是否明确定义。

关于c - "signed or unsigned type"在此 C90 未定义行为定义中的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917876/

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