C中为NULL [英] NULL in C

查看:69
本文介绍了C中为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C程序员在写作时被指责编写平台相关代码

int * x = 0;

他们必须写

int * x = NULL;

因为有些平台不代表NULL,因为所有位都为零。


C ++程序员似乎没有这个问题,只要他们想要
,他们就可以写0。为什么会这样?

C programmers get accused of writing platform dependant code when they write
int *x = 0;
They have to write
int *x = NULL;
because some platforms don''t represent NULL as all bits zero.

C++ programmers don''t seem to have this problem, they can write 0 whenever
they want. Why is that?

推荐答案

Serve La写道:
Serve La wrote:
C程序员在编写时被指责编写平台相关代码
int * x = 0;
他们必须写入
int * x = NULL;
因为有些平台不代表NULL,因为所有位都为零。
C programmers get accused of writing platform dependant code when they write
int *x = 0;
They have to write
int *x = NULL;
because some platforms don''t represent NULL as all bits zero.




0是一个空指针常量,当在指针上下文中使用时,无论实际位模式如何,都保证产生空指针




Jirka



0 is a null pointer constant and is guaranteed to yield a null pointer
when used in a pointer context, regardless of the actual bit pattern.

Jirka


" Serve La" < i@bleat.nospam.com>在消息中写道

news:bm ********** @ news1.tilbu1.nb.home.nl ...
"Serve La" <i@bleat.nospam.com> wrote in message
news:bm**********@news1.tilbu1.nb.home.nl...
C程序员被指控编写平台相关代码时写入
int * x = 0;
他们必须编写
int * x = NULL;
因为某些平台不代表NULL零点。

C ++程序员似乎没有这个问题,他们可以随时写出0。为什么?
C programmers get accused of writing platform dependant code when they write
int *x = 0;
They have to write
int *x = NULL;
because some platforms don''t represent NULL as all bits zero.

C++ programmers don''t seem to have this problem, they can write 0 whenever
they want. Why is that?




实际上,NULL应该是0.符合C编译器

识别值0(当用作指针时) )是一个

特殊值,表示NULL。即使实现

内部使用其他二进制值来表示NULL

指针,值0也是源代码使用的值。这个

包括赋值,比较,解除引用和转换

在整数和指针类型之间。编译器将

从源代码中的0转换为其内部表示

的NULL指针。


这就是为什么你可以写:


如果(x)

{

// x是非空的

}


与以下内容完全相同:


if(NULL!= x)

{

// x是非空的

}


也完全相同:


如果(0!= x)

{

// x是非空的

}


C编译器知道0正在指针类型

表达式中使用并做正确的事情。


-

------------------------------------------ ----

Jeffrey D. Smith

Farsight Systems Corporation

24 BURLINGTON DRIVE

LONGMONT,CO 80501 -6906

303-774-9381
http:/ /www.farsight-systems.com

z / Debug调试在IBM z / OS上运行的Systems / C程序!



Actually, NULL is supposed to be 0. Conforming C compilers
recognize the value 0 (when used as a pointer) to be a
special value that means NULL. Even when the implementation
internally uses some other binary value to mean a NULL
pointer, the value 0 is what is used the source code. This
includes assignment, comparison, dereferencing, and casting
between integer and pointer types. The compiler translates
from 0 in the source code into its internal representation
of a NULL pointer.

This is why you can write:

if(x)
{
// x is non-NULL
}

which is exactly the same as:

if(NULL != x)
{
// x is non-NULL
}

which is also exactly the same as:

if(0 != x)
{
// x is non-NULL
}

The C compiler knows that 0 is being used in a pointer-type
expression and does the right thing.

--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
303-774-9381
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!


2003年10月18日星期六14:53:03 GMT

" xarax" < xa的*** @ email.com>写道:
On Sat, 18 Oct 2003 14:53:03 GMT
"xarax" <xa***@email.com> wrote:
" Serve La" < i@bleat.nospam.com>在消息中写道
新闻:bm ********** @ news1.tilbu1.nb.home.nl ...
"Serve La" <i@bleat.nospam.com> wrote in message
news:bm**********@news1.tilbu1.nb.home.nl...
C程序员被指责编写平台相关代码当他们写int * x = 0;
他们必须写
int * x = NULL;
因为有些平台不代表NULL,因为所有位都为零。 c ++程序员似乎没有这个问题,他们可以随时写0
。为什么会这样?
实际上,NULL应该是0。
C programmers get accused of writing platform dependant code when
they write int *x = 0;
They have to write
int *x = NULL;
because some platforms don''t represent NULL as all bits zero.

C++ programmers don''t seem to have this problem, they can write 0
whenever they want. Why is that?
Actually, NULL is supposed to be 0.




实际上它不需要是0.(void *)0也是一个有效的定义

的NULL。

符合C编译器
认识值0(当用作指针时)是一个
特殊值,意味着空值。


只有一个常量表达式评估为0.我不认为整数

包含值0是由标准保证的,因为它将是一个

如果空指针不是全部为零,则实现很痛。

即使实现内部使用其他二进制值来表示NULL
指针,值为0是源代码使用的。这包括整数和指针类型之间的赋值,比较,解引用和转换。编译器将
从源代码中的0转换为NULL指针的内部表示。



Actually it is not required to be 0. (void*)0 is also a valid definition
of NULL.
Conforming C compilers
recognize the value 0 (when used as a pointer) to be a
special value that means NULL.
Only a constant expression evaluation to 0. I don''t think an integer
containing a value 0 is guaranteed by the standard because it would be a
pain to implement if the null pointer was not all bits zero.
Even when the implementation
internally uses some other binary value to mean a NULL
pointer, the value 0 is what is used the source code. This
includes assignment, comparison, dereferencing, and casting
between integer and pointer types. The compiler translates
from 0 in the source code into its internal representation
of a NULL pointer.




< snip>


其余的看起来是正确的。

-

马克戈登

支付成为一个极客&高级软件开发人员

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



<snip>

the rest looked about right.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.


这篇关于C中为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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