(!指针)和(指针!= NULL)之间的C差异,

时间:2022-12-15 19:30:36

I'm currently learning how to code C and stumbled upon an interesting line,

我目前正在学习如何编写C并偶然发现一条有趣的线,

Apparently,

struct node{
    node *next;
};

node *head = (node *)calloc(1, sizeOf(node));
head->next = NULL;

node *currentNode = head;
while (!currentNode)

In this context, is

在这方面,是

while(!currentNode)

different to

while(currentNode != NULL)

? I thought they meant to check for the same stuff, when current Node is not NULL but they are returning different results and I dont' understand...

?我认为他们的意思是检查相同的东西,当前的Node不是NULL,但是他们返回不同的结果,我不明白......

1 个解决方案

#1


while(currentNode)

and

while(currentNode != NULL)

are equivalent.

The first says that while currentNode has some value (could be anything, even garbage), the second one says that while currentNode is not NULL, i.e., has some value.

第一个说当currentNode有一些值(可能是任何东西,甚至是垃圾),第二个说当currentNode不是NULL,即有一些值。

On the other hand,

另一方面,

while(!currentNode)

means that while currentNode does not hold any value, i.e, is NULL.

表示虽然currentNode没有任何值,即为NULL。

#1


while(currentNode)

and

while(currentNode != NULL)

are equivalent.

The first says that while currentNode has some value (could be anything, even garbage), the second one says that while currentNode is not NULL, i.e., has some value.

第一个说当currentNode有一些值(可能是任何东西,甚至是垃圾),第二个说当currentNode不是NULL,即有一些值。

On the other hand,

另一方面,

while(!currentNode)

means that while currentNode does not hold any value, i.e, is NULL.

表示虽然currentNode没有任何值,即为NULL。