Reputation: 101
I'm curious as to why when say I traverse a linked list iteratively I must do while (list != NULL)
instead of while (!list)
. I thought NULL equated to zero or false.
From comments: My program seems to always crash when I attempt a while (!list)
but never the former. Each node contains a void pointer to a piece of data and a pointer to the next node.
Upvotes: 0
Views: 91
Reputation: 5238
In most cases, it's matter of style. Comparison to NULL
is more explicit.
As mentioned above, if(ptr != NULL)
is equivalent to if(ptr)
.
Upvotes: 1
Reputation: 72226
while (list != NULL)
is not the same as while (!list)
. They are opposites! Of course your program crashes, it tries to de-reference a NULL
pointer.
while (list != NULL)
is the same as while (list)
.
Upvotes: 5