0还是NULL? [英] 0 or NULL?

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

问题描述

如果我有一个指针并将其初始化为NULL它是否具有相同的效果

如果我将其初始化为0而不管它是什么类型的指针?

If I have a pointer and initialize it to NULL does it have the same effect
if I initialize it to 0 instead, no matter what kind of pointer it is?

推荐答案

Johs32认为:
Johs32 opined:
如果我有一个指针并将其初始化为NULL它是否具有相同的
效果如果我将它初始化为0而不管它是什么类型的指针?
If I have a pointer and initialize it to NULL does it have the same
effect if I initialize it to 0 instead, no matter what kind of
pointer it is?




是的,它由标准保证。


-

BR,Vladimir


schlattwhapper,n:

允许的窗帘本身被拉下来,

犹豫了一秒钟,然后啪的一声在你脸上。

- Rich Hall,Sniglets


2006年3月28日星期二21:35:32 +0200,在comp.lang.c,Johs32

< df *** @dsf .COM>写道:
On Tue, 28 Mar 2006 21:35:32 +0200, in comp.lang.c , Johs32
<df***@dsf.com> wrote:
如果我有一个指针并将其初始化为NULL它是否具有相同的效果
如果我将其初始化为0而不管它是什么类型的指针?
If I have a pointer and initialize it to NULL does it have the same effect
if I initialize it to 0 instead, no matter what kind of pointer it is?




是的,但是对于使用NULL更加用户友好,以便稍后维护程序员不会感到困惑。 />

Mark McIntyre

-

调试的速度是编写代码的两倍。

因此,如果您尽可能巧妙地编写代码,那么,根据定义,您可能不够聪明,无法调试它。

--Brian Kernighan



Yes, but its more user-friendly to use NULL, so that maintenance
programmers later on do not get confused.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan


Johs32写道:
如果我有一个指针并将其初始化为NULL它是否具有相同的效果
如果我初始化它改为0,不管它是什么类型的指针?
If I have a pointer and initialize it to NULL does it have the same effect
if I initialize it to 0 instead, no matter what kind of pointer it is?




是的。


然而,说0对比NULL,有一种常见的错误在许多系统上使用0而不是NULL时,
表面:为了将null

指针传递给可变参数函数(如printf()),你必须显式地转换

null指针常量指针类型。例如:


错误:

printf("%p",0);


这里0是作为整数传递,而不是指针。这将在系统上失败

其中指针和整数的大小不同,并且在

空指针不是全位零的系统上。


仍然错误:

printf("%p",NULL);


这仍然是错误的,因为NULL可以合法地定义为0,触发

同样的问题。但是,许多系统将NULL定义为((void *)0),其中
恰好起作用。压力碰巧。


右:

printf("%p",(void *)0);

printf("%p",(void *)NULL);


Variadic函数总是有这个问题,因为编译器无法检查

(或者更确切地说,不需要检查)参数类型。这同样适用于

a调用一个没有原型范围的函数(只是不做

)。


有关完整的故事(以及更多内容),请参阅常见问题解答的第5章:
http://www.c-faq.com/null/index.html


S. br />



Yes.

However, speaking of 0 versus NULL, there is a common kind of error that
surfaces when using 0 but not NULL on many systems: in order to pass a null
pointer to a variadic function (like printf()) you must explicitly cast the
null pointer constant to a pointer type. E.g.:

WRONG:
printf("%p", 0);

Here 0 is passed as an integer, not a pointer. This will fail on systems
where pointers and integers are not the same size and on systems where the
null pointer is not all-bits-zero.

STILL WRONG:
printf("%p", NULL);

This is still wrong because NULL can legally be defined as 0, triggering the
same problem. However, many systems define NULL as ((void*) 0), which
happens to work. Stress "happens to".

RIGHT:
printf("%p", (void*) 0);
printf("%p", (void*) NULL);

Variadic functions always have this problem because the compiler can''t check
(or rather isn''t required to check) the argument types. The same applies to
a call to a function which doesn''t have a prototype in scope (just don''t do
that).

For the full story (and a whole lot more), see chapter 5 of the FAQ:
http://www.c-faq.com/null/index.html.

S.


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

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