Interesting issue we came across here this week.
We are working in C on a Harvard Architecture embedded platform, which has 16-bit data addresses and 32-bit code addresses.
The issue occurs when you are working with function pointers. If you have code like
if (fp) fp();
if (fp != 0) fp();
if (fp != NULL) fp();
(void *) 0
It seems to me the easiest way is replace all occurrences of
0. This works for function pointer (as you say) and object pointers.
This is a variant of (2) Redefine NULL to plain
But the fact that you cannot compare function pointers with
NULL is a bug in your implementation. C99 states that comparison of the null pointer constant is possible with both object and function pointers, and that NULL should expand to this constant.
Small addition from the C-FAQ question 5.8:
Q: Is NULL valid for pointers to functions?
A: Yes (but see question 4.13)
(void *) 0
(A reply to R..'s comment). I believe using function pointers and
(void *) 0 together is well-defined. In my reasoning I will refer to sections of the C99 draft 1256, but will not quote large parts to keep it readable. It should also be applicable to C89.
0and such an expressions cast to
(void *)as null pointer constant. And: "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."
!=operands for (among other) a pointer operand and a null pointer constant. For these: "If one operand is a pointer and the other is a null pointer constant, the null pointer constant is converted to the type of the pointer."
fp == (void *) 0, the null pointer constant is converted to the type of
fp. This null pointer can be compared to
fp and is guaranteed to be unequal to
fp if it points to a function. Assignment (
=) has a similar clause, so
fp = (void *) 0; is also well-defined C.