NULL 被我的编译器 std lib 定义为 0:是否正确? [英] NULL defined as 0 by my compiler std lib: is it correct?

查看:53
本文介绍了NULL 被我的编译器 std lib 定义为 0:是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 C 编译器的标准库这样定义 NULL:

The standard library of my C compiler defines NULL this way:

#define NULL 0

我希望:

#define NULL ((void *)0)

谁能告诉我哪个是正确的,为什么?

Could someone tell me which one is correct and why?

谢谢!

推荐答案

C 标准的最新修订版 C99 和 C11,将空量等同于零或空指针.
来自 ISO/IEC 9899:201x §6.3.2.3 指针,第 3 点:

The last revisions of C standard, C99 and C11, equates the null quantity to zero or null pointer.
From ISO/IEC 9899:201x §6.3.2.3 Pointers, point 3:

值为 0 的整数常量表达式,或这样的表达式强制转换为 void * 类型,称为空指针常量.如果一个空指针常量被转换为指针类型,结果指针,称为空指针,保证比较不等于指向任何对象或函数的指针.

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.

所以你看到的是一个完全符合 C99-C11 编译器的 NULL 声明,而不是以前的 C99 编译器的 #define NULL ((void *)0).

So what you see is a perfectly compliant NULL declaration of a C99-C11 compiler as opposed to the previous #define NULL ((void *)0) of pre C99 compilers.

这篇关于NULL 被我的编译器 std lib 定义为 0:是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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