为什么空并不$ P $由编译器pdefined [英] Why NULL is not predefined by the compiler

查看:112
本文介绍了为什么空并不$ P $由编译器pdefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题困扰了我一段时间。我从来没有见过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 type void *, 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 macro NULL 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 to void *, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆