Riya Jang
Riya Jang

Reputation: 13

C difference between (!pointer) and (pointer != NULL),

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

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...

Upvotes: 0

Views: 100

Answers (1)

Ayushi Jha
Ayushi Jha

Reputation: 4023

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.

On the other hand,

while(!currentNode)

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

Upvotes: 3

Related Questions