In PJ Plauger's book, The Standard C Library, he warns about assigning a pointer to a NULL function.
In particular, he says the following:
The NULL macro serves as an almost universal null pointer constant. You use it as the value of a data object pointer, which should indicate the absence of a data object declared (or allocated) in the program. As I mentioned on page 216, a macro can have any of the following definitions 0, 0L, or (void *) 0.
The latter definition is compatible with any pointer to a data object. However, it is not compatible with a function pointer. That means you cannot write
int (*pfun) (void) = NULL; /* WRONG */
The translator may complain that the type of expression is incompatible with the data object that you want to initialize.
He goes on to say that:
... However, there is no guarantee that the void pointer has the same representation as any other (non-character) pointer. This is not even a compatible assignment with function pointers. This means that you cannot write NULL as the universal constant of a null pointer . You can also safely use it as an argument expression instead of an arbitrary data object pointer.
I have been setting pointers to NULL for some time without any problems, and I am wondering if it is portable.
In particular:
void (*test)() = NULL => compiles with both gcc and g ++
void (*test)() = 0 => compiles with both gcc and g ++
void (*test)() = (void*)0 => produced an invalid conversion error in both gcc and g ++
EDIT: void (*test)() = (void*)0 compiles in gcc, I used a file with the extension .cpp ... Nevertheless, will it always compile, despite the fact that Plauger says that the pointer is assigned functions on NULL is wrong?
The part I don't understand is the definition of NULL in my stddef.h:
#if defined (_STDDEF_H) || defined (__need_NULL) #undef NULL /* in case <stdio.h> has defined it. */ #ifdef __GNUG__ #define NULL __null #else /* G++ */ #ifndef __cplusplus #define NULL ((void *)0) // this line confuses me #else /* C++ */ #define NULL 0 #endif /* C++ */ #endif /* G++ */ #endif /* NULL not defined and <stddef.h> or need NULL. */ #undef __need_NULL
It seems that the definition of NULL is 0 in C ++ and ((void *) 0) in C. Is it really or defined as __null?
EDIT: If so, why does the NULL assignment work all the time, although assigning (void *) 0, according to Plauger, is "wrong"?
EDIT 2: I'm interested in C89