gcc and NULL function pointers.

Norman Diamond diamond at jit533.swstokyo.dec.com
Tue Jun 4 11:29:14 AEST 1991


In article <16386 at ganymede.inmos.co.uk> conor at inmos.co.uk () writes:
>  void (*ptr)(void); /* pointer to void function returning void */
>  ptr = (void *)0;                   /* 2 */
>  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
>
>ANSI standard states (section 3.3.16.1) that a constraint on simple assignment
>"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.

I agree.  A null pointer constant of type (void *) has a certain privilege
which is not inherited from either of its parent categories; it is compatible
with function pointer types as well as with object pointer types.

(Non-constant null pointers of type (void *) are compatible only with object
pointer types, and constant non-null pointers of type (void *) are also
compatible only with object pointer types.)

Now to be pedantic, my statement might be too broad.  If an integral constant
expression with value 0 is cast to type (void *), it is a null pointer
constant.  But if a null pointer constant is cast to type (void *), is it still
a null pointer constant?  It is still constant (I think) and is still a null
pointer, but does it still have its extra privilege?  The standard doesn't say!
  ptr = (void *)0;          /* Conversion to type of function pointer, yes */
  ptr = (void *)(void*)0;   /* Not defined! */
>On our system, NULL is #defined in a header file to be ((void *)0),
So, be careful casting your uses of the NULL macro!!
--
Norman Diamond       diamond at tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Permission is granted to feel this signature, but not to look at it.



More information about the Comp.std.c mailing list