0

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.

Synlar
  • 101
  • 6
  • 3
    you'll get the same result from both of them – Pooya Apr 06 '16 at 09:17
  • a matter of style. the first is more clear to the reader. – David Haim Apr 06 '16 at 09:18
  • @nos my program seems to always crash when I attempt `!NULL`. It is a valid list where each node contains a void pointer to a piece of data and a pointer to the next node. – Synlar Apr 06 '16 at 09:19
  • @nos All I'm doing is traversing the linked list. Each time doing `list = list -> next`. I'm so out of ideas then... especially because it works with one but not the other. – Synlar Apr 06 '16 at 09:26
  • 1
    [NULL is **not** zero](http://stackoverflow.com/q/2597142/995714). `!NULL` is 1 so you are almost always receive segfault because 1 is not a valid address on most systems – phuclv Apr 06 '16 at 09:27
  • @Pooya Disagree with "you'll get the same result from both of them" `list != NULL` is not the same as `!list`. – chux - Reinstate Monica Apr 06 '16 at 13:56
  • @David Haim "..."a matter of style." --> `while (list != NULL)` and `while (!list)` has a different style and more importantly a different functionality. – chux - Reinstate Monica Apr 06 '16 at 13:58

2 Answers2

5

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

axiac
  • 68,258
  • 9
  • 99
  • 134
  • 2
    When you spend days in a dungeon trying to get the same function working you start to forget basic things. I hope this helps future comrades undertaking the same course I am. Thanks. – Synlar Apr 06 '16 at 09:33
  • Impressed that OP, 2 commenters and 3 up-votes to those comments all missed that the 2 code fragments had opposite functionality. (Maybe would have missed it myself had I not read this answer first.) Suggest that it was brought on by the confusion of comparing two statements with `!` in them. IMHO using `while (list)` is best. Or using `while (list == NULL)` when code needs to do the opposite. Both do not use `!`. – chux - Reinstate Monica Apr 06 '16 at 14:19
1

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

George Sovetov
  • 4,942
  • 5
  • 36
  • 57
  • 2
    This is not correct. You are mixing up the NULL macro with null pointers. NULL is _always_ `0` or `(void*)0`. [Read this](http://stackoverflow.com/questions/32136092/how-to-write-c-c-code-correctly-when-null-pointer-is-not-all-bits-zero/32136460#32136460). – Lundin Apr 06 '16 at 09:31
  • @Lundin I did some googling. Yes, that was a gap in my knowledge. I'll remove this part. – George Sovetov Apr 06 '16 at 09:48
  • 1
    @Lundin Certainty `NULL` macro could be `0` or `(void*)0`. Yet the C spec does not defined it as only those two. Others are possible including `((void*)0)`, `(-0)`, `((intptr_t) 0)`. – chux - Reinstate Monica Apr 06 '16 at 14:09
  • 1
    @chux My bad, for some reason I thought you had written `(intptr_t*)`. You are right, I'll delete the comment. – Lundin Apr 07 '16 at 13:16