问题描述
只是为了检查,如果我将指针显式设置为NULL,我不允许
取消引用它?为什么会这样,它不是像它指向任何垃圾一样?为什么还要将其设置为NULL。我可以
记住一些实例,其中
i就是这样,而printf报告的内容如下:(null)。
我''我问,因为我在某处读到它无效。我认为
根本无法取消引用
a指针,它不会指向任何东西。
但是我可以检查它是否为NULL或不对吗? (我希望):-)
Rgds,
-alef
Hi,
Just to check, if i set a pointer explicitly to NULL, i''m not allowed
to dereference it? Why is that, it''s not
like it''s pointing to any garbage right? Why else set it to NULL. I can
remember some instances where
i did just that and printf reported something like : (null).
I''m asking because i read somewhere that it is not valid. I thought it
simply wasn''t valid to dereference
a pointer who is _not_ set to point to anything.
But i can check if it is NULL or not right ? ( i hope ) :-)
Rgds,
-alef
推荐答案
正确。
Right.
它没有指向任何东西,所以我想从这个角度来看
它并没有指向任何垃圾。 ;-)
It isn''t pointing to anything, so I guess from that point of view
it isn''t pointing to any garbage. ;-)
但是NULL指针 - 已设置为不指向任何内容。 NULL
指针是-defined-,不指向任何有效对象。
取消引用它的意思是什么?这种情况在哲学上类似于取消引用void *指针 - 没有是那里!
-
没有人有权通过要求经验证据来摧毁另一个人的信念。 - Ann Landers
But a NULL pointer -has- been set not to point to anything. The NULL
pointer is -defined- as not pointing to any valid object. What would
it mean to dereference it? The situation is philosophically similar
to dereferencing a void* pointer -- there is no "is" there!
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers
你_can_,但行为未定义,因为在C中,NULL指针
保证不指向任何有效对象。
You _can_, but the behaviour is undefined, since in C, a NULL pointer
is guaranteed to not point to any valid object.
将未初始化的指针设置为NULL是一种很好的做法。还有一些
函数需要一个空指针。许多编程结构需要一个
空指针作为标记值。许多函数返回一个空指针
作为失败的指示。
< snip>
Setting an uninitialised pointer to NULL is a good practise. Also some
functions require a null pointer. Many programming constructs require a
null pointer as a sentinel value. Many functions return a null pointer
as an indication of failure.
<snip>
是。
Yes.
是的,你可以。
Yes, you can.
这篇关于检查指针是否允许NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!