C:对比为NULL [英] C: Comparison to NULL
问题描述
宗教争论放在一边:
-
选项1:
Option1:
if (pointer[i] == NULL) ...
选项2:
Option2:
if (!pointer[i]) ...
在C是选项1功能上等同于选项2?
In C is option1 functionally equivalent to option2?
请问以后解决更快,由于缺乏比较的?
Does the later resolve quicker due to absence of a comparison ?
推荐答案
我喜欢第二个,其他的人都喜欢第一。
I like the second, other people like the first.
其实,我preFER第三类到第一种:
Actually, I prefer a third kind to the first:
if (NULL == ptr) {
...
}
由于当时我:
- 将不能错过,只是第一类=
- 不会错过== NULL,并误以为是相反的,如果条件是长(多线)
功能它们是等价的。
即使 NULL
指针不为0(全零位),如果(!PTR)
与之相比,在 NULL
指针。
Even if a NULL
pointer is not "0" (all zero bits), if (!ptr)
compares with the NULL
pointer.
以下是不正确。它仍然在这里,因为有很多的意见提到它:
不要比较字面零指针,但是。它将工作几乎无处不在,但不确定的行为IIRC。
The following is incorrect. It's still here because there are many comments referring to it: Do not compare a pointer with literal zero, however. It will work almost everywhere but is undefined behavior IIRC.
这篇关于C:对比为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!