gpt4 book ai didi

c - 标准是否定义空指针常量以将所有位设置为零?

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:16 24 4
gpt4 key购买 nike

(我引用的是 ISO/IEC 9899:201x)

这里我们看到,整数常量表达式有一个整数类型:

6.6 Constant expressions

6.An integer constant expression shall have integer type and shall only have operandsthat are integer constants, enumeration constants, character constants, sizeofexpressions whose results are integer constants, _Alignof expressions, and floatingconstants that are the immediate operands of casts. Cast operators in an integer constantexpression shall only convert arithmetic types to integer types, except as part of anoperand to the sizeof or _Alignof operator.

那么这对任何整数类型都适用:

6.2.6.2 Integer types

5.The values of any padding bits are unspecified.A valid (non-trap) object representationof a signed integer type where the sign bit is zero is a valid object representation of thecorresponding unsigned type, and shall represent the same value. For any integer type,the object representation where all the bits are zero shall be a representation of the valuezero in that type.

然后我们看到空指针常量是使用值为 0 的整数常量表达式定义的。

6.3.2.3 Pointers

3.An integer constant expression with the value 0, or such an expression cast to typevoid*, is called a null pointer constant. If a null pointer constant is converted to apointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequalto a pointer to any object or function.

因此空指针常量必须将其所有位设置为零。

但是在线和 StackOverflow 上有很多答案表明这不是真的。

鉴于引用的部分,我很难相信他们。

(请引用最新标准回答)

最佳答案

Does Standard define null pointer constant to have all bits set to zero?

不,它没有。 C 标准中没有任何段落强加这样的要求。

void *p = 0;
例如,

p 是一个空指针,但标准不要求对象 p 必须设置所有位。

有关信息,c-faq 网站在此处提到了一些具有非零空指针表示的系统:http://c-faq.com/null/machexamp.html

关于c - 标准是否定义空指针常量以将所有位设置为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27714377/

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