I want initialize a function pointer so that it is null. Which of these two ways is preferred?
void (*Pointer)(void) = NULL;
Or
void (*Pointer)(void) = (void (*)(void))0;
0 is implicit convertible to any pointer type. Though how your compiler implements NULL depends.
In your code you can simply write
void (*Pointer)(void) = 0; but it won't be portable , so write void (*Pointer)(void) = NULL;
The standard (ISO/IEC 9899:2011) says:
§6.3.2.3 Pointers
¶1 A pointer to
voidmay be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer tovoidand back again; the result shall compare equal to the original pointer.¶2 For any qualifier q, a pointer to a non-q-qualified type may be converted to a pointer to the q-qualified version of the type; the values stored in the original and converted pointers shall compare equal.
¶3 An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant.66) 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.¶4 Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.
…
¶8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.
66) The macro
NULLis defined in<stddef.h>(and other headers) as a null pointer constant; see 7.19.§7.19 Common definitions
<stddef.h>¶3 The macros are
NULLwhich expands to an implementation-defined null pointer constant; …
All of which adds up to say that either of the notations used in the question is valid and they (and other similar expressions) are equivalent and there will be no problem.
void (*Pointer)(void) = NULL;
void (*Pointer)(void) = (void (*)(void))0;
void (*Pointer)(void) = 0;
Do note that the standard doesn't explicitly say you can convert a pointer to an object type into a pointer to a function type, or vice versa, or that round-tripping is guaranteed to work. Remember, there were once memory models for early Intel machines where data (object) pointers were a different size from function pointers — and that can still happen on other machines (IBM AS/400 and its heirs and successors is another example).
You could also study the questions and answers on the null pointer from http://c-faq.com/.
NULL is defined to be a null pointer constant. A null pointer constant can be used to initialise any pointer (data pointer or function pointer) to a null pointer of the correct type, so assigning NULL is just fine.
0 will also work, because 0 is also a null pointer constant. So will 0L, (13*12-156) and many other ways. NULL is preferred because it shows your intent to assign a null pointer.
The line below that is just unnecessary work.
void *, is called a null pointer constant.
NULLis0(though I do not know of any implementation where that is not the case).NULLwhich is exactly designed for this.