As far as i know, the NULL is actually not 0. So is there any difference in comparing a pointer with 0 or with NULL? Further what should be the correct usage. Thanks!
- 
                    1Is there any reason you would want to use `0` instead of `NULL`? – Gabe Jul 08 '13 at 12:51
- 
                    2No, either is fine. Best (in my opinion) is not to use either butjust use ! Or use the pointer itself as a controlling expression. – R.. GitHub STOP HELPING ICE Jul 08 '13 at 12:52
- 
                    4http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0 – dijkstra Jul 08 '13 at 12:52
- 
                    @Gabe I came across a piece of code which had compared a pointer to 0. Hence the question – Shash Jul 08 '13 at 12:55
3 Answers
The correct usage is to use NULL : It's more readable (p == NULL -> you know that p is a pointer)
- 
                    6"Correct" is a bit strong here. It is more a matter of style. Even if NULL ifs not zero, it is guaranteed to evaluate as equivalent. – lnafziger Jul 08 '13 at 13:11
- 
                    I see wrong descriptions here. It may happen that the NULL pointer's binary representation doesn't consist of all 0 bits, but even then, `NULL == 0`. – glglgl Jul 08 '13 at 13:36
In C,the macro NULL is defined as an implementation-defined null pointer constant, which in C99 can be portably expressed as the integer value 0 converted implicitly or explicitly to the type void*.
In C++ NULL is the integer literal for zero (0 or 0L) has been traditionally preferred to represent a null pointer constant.
Compiler would implicitly convert 0 to NULL in case of comparison with a pointer.
It is always safe to compare 0 with NULL.
 
    
    - 2,305
- 2
- 23
- 40
- 
                    2"Most compilers would implicitly convert 0 to NULL in case of comparison with a pointer." Actually, this is required by the standard, so it should be "all compilers". – Dietrich Epp Jul 08 '13 at 18:27
- 
                    1
NULL is not necessarily 0 and should only be used for pointers. NULL is a macro usually defined as (void*)0 but not always.
It's a pointer to no location in memory.
There's a very good new book called Understanding And Using C Pointers. Please read that one.
Don't use it for anything but pointers.
 
    
    - 12,679
- 6
- 37
- 55
- 
                    
- 
                    how is that different than what i wrote? NULL compares equal to zero but it is not necessarily actually 0. – uchuugaka Jul 08 '13 at 15:50
- 
                    You don't say in your answer that NULL compares as equal to zero. That's what is different. – lnafziger Jul 09 '13 at 01:58
- 
                    Well it does not invalidate my answer in the least. I state fact that it is not always zero. – uchuugaka Jul 09 '13 at 02:53
- 
                    1@uchuugaka "...that it is not always *represented by* zero." That makes a difference. – glglgl Jul 09 '13 at 07:24
- 
                    Whatever. Now you're totally stepping into semantic nonsense. NULL is a macro that represents something not the other way around. – uchuugaka Jul 09 '13 at 09:54
 
     
     
    