Reputation: 3470
The standard library of my C compiler defines NULL
this way:
#define NULL 0
I would expect:
#define NULL ((void *)0)
Could someone tell me which one is correct and why?
Thank you!
Upvotes: 1
Views: 270
Reputation: 410
In C language, the two way you said are both correct, both of they can be convert to other pointer that point to whatever object(any type) automatically, but in C++ language, #define NULL ((void *)0)
is wrong, because void* can't be convert to other pointer automatically just like C.
Upvotes: 4
Reputation: 4877
The last revisions of C standard, C99 and C11, equates the null quantity to zero or null pointer.
From ISO/IEC 9899:201x §6.3.2.3 Pointers, point 3:
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
So what you see is a perfectly compliant NULL declaration of a C99-C11 compiler as opposed to the previous #define NULL ((void *)0)
of pre C99 compilers.
Upvotes: 6