空指针问题 [英] null pointers questions

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

问题描述

我已经读过0是''空指针常量'',并将它指定给

指针使它成为''空指针''。真的吗?并且相反

是真的吗? (指定一个指向int的空指针将int设置为0)?或者应该

我用if(指针!= NULL)替换所有if(指针)?另一个问题:

如何评估NULL?

-

Devaraja(Xdevaraja87 ^ gmail ^ c0mX)

Linux注册用户#338167
http://counter.li.org

我读过0是''空指针常量'',并指定它指向
指针使其成为空指针。真的吗?相反的是真的吗? (指定一个指向int的空指针将int设置为0)?或者应该用if(指针!= NULL)替换所有if(指针)?另一个问题:
如何评估NULL?




在标准方面不确定,但在每个编译器中我都没有

使用NULL = 0,并且是可互换的。 if(指针)和if(指针!= 0)

在处理指针时是等价的。


对我来说,设置为NULL vs 0更好,因为它显示了你想要做什么
(将指针绑定到任何东西)。对于评估,我仍然使用if(指针),因为我读它就好像指针存在,而不是如果指针不是0,那么
是微不足道的小事,但有助于人们

正在查看你的代码。


DevarajA写道:

我读过那个0是''空指针常量'',并将它指定给
指针使它成为''空指针''。真的吗?


是的。

并且相反
是真的吗? (指定一个指向int的空指针将int设置为0)?


不,如果没有特别演员,这是不允许的。

或者应该
我用if替换所有的if(指针)指针!= NULL)?另一个问题:
如何评估NULL?




''NULL''是一个宏。它通常只有0.您当然可以将任何

指针与0进行比较,两者都将转换为_pointers_,反之亦然。你b $ b也可以使用*指针*,其中一个逻辑值是预期的,然后是null

指针将评估为''false''而非null将评​​估为'' 'true''。

所以你可以说


如果(指针)


这是相同的


if(指针!= 0)

V


" Josh麦克法兰" <哒***** @ gmail.com>在新闻中写道:1126214594.473166.233130

@ g44g2000cwa.googlegroups.com:

对我来说,设置为NULL对0更好是因为它显示了你的内容
正在尝试(将指针绑定到任何东西)。对于评估,我仍然使用if(指针),因为我读它就好像指针存在,而不是指针不是0。这是微不足道的事情,但当人们看着它时有帮助你的代码。




我实际上更喜欢反过来(对于if比较)。我不会像隐式测试一样b $ b ...所以我只会使用:


if(variable)

>
如果变量是bool。在所有其他情况下,我更喜欢明确地测试它。

(明确地将int'与0进行比较,或者将指针与NULL进行比较)。


if(intvar != 0)


......或者......


if(ptr!= NULL)

>


I''ve read that 0 is the ''null pointer constant'', and assigning it to a
pointer makes it a ''null pointer''. Is that true? And is the opposite
true? (assigning a null pointer to an int sets the int to 0)? Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
how does NULL evaluate?
--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org

解决方案

DevarajA wrote:

I''ve read that 0 is the ''null pointer constant'', and assigning it to a
pointer makes it a ''null pointer''. Is that true? And is the opposite
true? (assigning a null pointer to an int sets the int to 0)? Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
how does NULL evaluate?



Not sure on the standard side of things, but in every compiler I''ve
used NULL = 0, and are interchangable. if(pointer) and if(pointer != 0)
are equivalently the same thing when dealing with pointers.

For me, setting things to NULL vs 0 is better because it shows what you
are attempting to do (bind a pointer to nothing). For evaluations, I
still use if(pointer) because I read it as if pointer exists, rather
than if pointer is not 0. Minor trivial things, but helps when people
are looking at your code.


DevarajA wrote:

I''ve read that 0 is the ''null pointer constant'', and assigning it to a
pointer makes it a ''null pointer''. Is that true?
Yes.
And is the opposite
true? (assigning a null pointer to an int sets the int to 0)?
No, that''s not allowed without a special cast.
Or should
I replace all my if(pointer) with if(pointer!=NULL)? Another question:
how does NULL evaluate?



''NULL'' is a macro. It usually is just 0. You can of course compare any
pointer to 0, both will be converted to _pointers_, not vice versa. You
can also use a *pointer* where a logical value is expected, then null
pointers will evaluate to ''false'' and non-null will evaluate to ''true''.
So you can say

if (pointer)

which is the same as

if (pointer != 0)

V


"Josh Mcfarlane" <da*****@gmail.com> wrote in news:1126214594.473166.233130
@g44g2000cwa.googlegroups.com:

For me, setting things to NULL vs 0 is better because it shows what you
are attempting to do (bind a pointer to nothing). For evaluations, I
still use if(pointer) because I read it as if pointer exists, rather
than if pointer is not 0. Minor trivial things, but helps when people
are looking at your code.



I actually prefer the other way around (for the if comparisions). I don''t
like implicit tests... so I''ll only use:

if (variable)

if variable is a bool. In all other cases I prefer to explicitly test it.
(comparing int''s against 0 explicitly, or pointer against NULL).

if (intvar != 0)

.... or...

if (ptr != NULL)


这篇关于空指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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