As far as I know, C defines NULL
like this:
#define NULL ( (void *) 0)
Then, how should we define NULL_POINTER
? I defined it the same in my program and it worked, but I suppose that is just a coincidence:
#define NULL_POINTER ( (void *) 0)
What would be the logical definition, if any ?
Brian Tompsett - 汤莱恩
5,92972 gold badges63 silver badges135 bronze badges
asked Sep 13, 2013 at 8:18
1 Answer 1
#define NULL ( (void *) 0)
and
#define NULL 0
are both valid. If you need to implement your own macro for null pointer, the same rule applies.
C11(ISO/IEC 9899:201x) §6.3.2.3 Pointers Section 3
An integer constant expression with the value
0
, or such an expression cast to typevoid *
, is called a null pointer constant
answered Sep 13, 2013 at 8:20
2 Comments
Yu Hao
@PaulR According to port70.net/~nsz/c/c99/n1256.html#6.3.2.3, it's the same in C99. Although I do agree that the first definition is better.
Piovezan
The first definition seems better to me since in some compilers comparing a numeric value to NULL results in a warning (e.g. "[Warning] comparison between pointer and integer") while the second definition doesn't. Thus, the first definition provides better semantics.
lang-c
NULL
is a null pointer, why do you want a separate define for that?