10

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
2
  • 13
    NULL is a null pointer, why do you want a separate define for that? Commented Sep 13, 2013 at 8:21
  • 3
    You should leave the definition for the implementation. Defining your own version of NULL can only make things worse. Commented Sep 13, 2013 at 8:26

1 Answer 1

18
#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 type void *, is called a null pointer constant

answered Sep 13, 2013 at 8:20

2 Comments

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.