gcc and NULL function pointers.

Conor O'Neill conor at lion.inmos.co.uk
Mon Jun 3 20:39:28 AEST 1991


Consider the following function:

void f(void)
{
  void (*ptr)(void); /* pointer to void function returning void */

  ptr = 0;                           /* 1 */
  ptr = (void *)0;                   /* 2 */
  ptr = (void (*)(void))0;           /* 3 */
  ptr = (void (*)(void))((void *)0); /* 4 */
}

When compiled with gcc version 1.39, with -ansi -pedantic,
it reports a warning on the line marked /* 2 */:
c.c:6: warning: assignment between incompatible pointer types
(Note, without -pedantic gcc is silent)

On our system, NULL is #defined in a header file to be ((void *)0),
thus gcc will give a warning when attempting to set a function pointer
to NULL.

ANSI standard states (section 3.3.16.1) that a constraint on simple assignment
is that (...) "the left operand is a pointer and the right is a null pointer
constant".

The index points me to section 3.2.2.3 for a definition:
"An integral constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.
If a null pointer constant is assigned to or compared for equality to a
pointer, the constant is converted to a pointer of that type"

Thus, I claim that /* 2 */ should be converted by the compiler to /* 4 */
and thus should not create a warning. Hence gcc is incorrect in this point.

---
Conor O'Neill, Software Group, INMOS Ltd., UK.
UK: conor at inmos.co.uk		US: conor at inmos.com
"It's state-of-the-art" "But it doesn't work!" "That is the state-of-the-art".



More information about the Comp.std.c mailing list