gpt4 book ai didi

c - C语言中为什么会有NULL?

转载 作者:行者123 更新时间:2023-12-05 08:25:50 26 4
gpt4 key购买 nike

C语言中为什么会有NULL?是否存在仅普通文字 0 无法完全相同的上下文?

最佳答案

实际上,您可以在任何需要使用 NULL 的地方使用文字 0

C standard 的第 6.3.2.3p3 节状态:

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

第 7.19p3 节指出:

The macros are:

NULL

which expands to an implementation-defined null pointer constant

因此 0 符合空指针常量,(void *)0NULL 也是如此。但是,首选使用 NULL,因为它让读者更清楚地知道正在使用的是空指针,而不是整数值 0。

关于c - C语言中为什么会有NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61718737/

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