1

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;
9
  • 1
    Technically, I don't think the standard guarantees that NULL is 0 (though I do not know of any implementation where that is not the case). Commented Dec 22, 2015 at 17:32
  • There's a guarantee that NULL is a null pointer constant, that's all you need to know. Commented Dec 22, 2015 at 17:33
  • If you really want it to be zero and not NULL, do it the second way, otherwise the first. The only time you'll probably want that is if you are writing kernel code or working on embedded systems. Commented Dec 22, 2015 at 17:33
  • 3
    @R_Kapp: that's true, but it guarantees that it is a null pointer constant, and also that 0 suitably cast is also a null pointer constant, so the net result is essentially the same. Commented Dec 22, 2015 at 17:34
  • 2
    So, if you want a null pointer, you should use a null pointer constant. The recommended version for C is to use the macro NULL which is exactly designed for this. Commented Dec 22, 2015 at 17:38

3 Answers 3

3

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;

Sign up to request clarification or add additional context in comments.

2 Comments

Why wouldn't it be portable?
@rici well it will be portable but second one is more readable and reduces mental friction more likely :D
2

The standard (ISO/IEC 9899:2011) says:

§6.3.2.3 Pointers

¶1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and 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 NULL is 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

NULL

which 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/.

Comments

1

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.

2 Comments

"0 will also work ..." can be understood you refer to the second construct in the question.
For clarification, per the C Standard 6.3.2.3 Pointers An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

Your Answer

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.