title: Can I reliably set a function pointer to NULL in C and C++?
body: <p>In P.J. Plauger's book, <em>The Standard C Library</em>, he warns about assigning a function pointer to NULL.</p>
  <p>Specifically, he says this:</p>
  <blockquote>
  <p>The macro <strong>NULL</strong>  serves as an almost-universal null pointer constant. You use it as the value of a data-object pointer that should point to no data object declared (or allocated) in the program. As I mentioned on page 216, the macro can have any of the following definitions 0, 0L, or (void *)0.</p>
  </blockquote>
  <blockquote>
  <p>The last definition is compatible with any data object pointer. It is <em>not</em>, however, compatible with a function pointer. That means you cannot write</p>
  </blockquote>
  <blockquote>
  <p><code>int (*pfun) (void) = NULL;    /* WRONG */</code></p>
  </blockquote>
  <blockquote>
  <p>The translator may complain that the expression type is incompatible with the data object you wish to initialize.</p>
  </blockquote>
  <p>He goes on to say that:</p>
  <blockquote>
  <p>...There is no guarantee, however, that a pointer to void has the same <em>representation</em> as any other (non-character) pointer. It isn't even assignment compatible with function pointers. <strong>That means that you can't write NULL as a universal null-pointer constant</strong>. Nor can you safely use it as an argument expression in place of an arbitrary data-object pointer.</p>
  </blockquote>
  <p>I have been assigning function pointers to <code>NULL</code> for quite some time without any problems, and I am wondering if it isn't portable.</p>
  <p>Specifically:</p>
  <p><code>void (*test)() = NULL</code> =&gt; compiles fine with both gcc and g++</p>
  <p><code>void (*test)() = 0</code> =&gt; compiles fine with both gcc and g++</p>
  <p><strike><code>void (*test)() = (void*)0</code> =&gt; produced an invalid conversion error in both gcc and g++</strike></p>
  <p><strong>EDIT: <code>void (*test)() = (void*)0</code>compiles fine in gcc, I was using a file with a .cpp extension...
  Still, will it always compile, despite Plauger saying that assigning a function pointer to <code>NULL</code> is wrong?</strong></p>
  <p>The part I don't understand is the definition of NULL in my stddef.h:</p>
  <pre><code>#if defined (_STDDEF_H) || defined (__need_NULL)
  #undef NULL     /* in case &lt;stdio.h&gt; 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 &lt;stddef.h&gt; or need NULL.  */
  #undef  __need_NULL
  </code></pre>
  <p>This seems to be defining <code>NULL</code> to be 0 in C++ and ((void *)0) in C. Is it really, or is it being defined as __null?</p>
  <p>If so, why does assigning to <code>NULL</code> work all the time, even though assigning to (void*)0,  according to Plauger, is &quot;wrong&quot;?**</p>
  <p>I am interested in C89**</p>