为什么空并不$ P $由编译器pdefined [英] Why NULL is not predefined by the compiler
问题描述
这个问题困扰了我一段时间。我从来没有见过NULL的不同的定义,它总是
的#define NULL((无效*)0)
有其中null是diferently定义的任何建筑,如果是这样,为什么编译器不声明此为我们?
Ç2011标准,的在线草案
6.3.2.3指针的结果
...结果
3值为0,或者这样的前pression投整型常量前pression键入无效*
,被称为的空指针常量的 66)如果空指针常量将被转换为
指针类型,所得到的指针,称为空指针的,是保证比较不等的指针的任何对象或功能。
66)宏NULL
是德网络&LT定义; STDDEF.H>
(以及其他头)作为空指针常数;见7.19。
块引用>的宏的
NULL
是总是的定义为一个零值恒前pression;它可以是一个赤裸裸的0或0投地无效*
,或其他一些积分前pression计算结果为0。至于你的来源$ C $ç的来讲,NULL
将始终评估为0。一旦code已被翻译,空指针不断出现的任何(0
NULL
等)将与任何基础架构使用替代为空指针,其可以或可以不0值。This issue bothered me for a while. I never saw a different definition of NULL, it's always
#define NULL ((void *) 0)
is there any architecture where NULL is defined diferently, and if so, why the compiler don't declare this for us ?
解决方案C 2011 Standard, online draft
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.66) 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.
66) The macroNULL
is defined in<stddef.h>
(and other headers) as a null pointer constant; see 7.19.The macro
NULL
is always defined as a zero-valued constant expression; it can be a naked 0, or 0 cast tovoid *
, or some other integral expression that evaluates to 0. As far as your source code is concerned,NULL
will always evaluate to 0.Once the code has been translated, any occurrence of the null pointer constant (0,
NULL
, etc.) will be replaced with whatever the underlying architecture uses for a null pointer, which may or may not be 0-valued.这篇关于为什么空并不$ P $由编译器pdefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!