is (int (*)())NULL legal when NULL is (void *)0?

Doug Gwyn gwyn at smoke.brl.mil
Fri Nov 16 05:23:49 AEST 1990


In article <13799 at crdgw1.crd.ge.com> volpe at camelback.crd.ge.com (Christopher R Volpe) writes:
-In article <14457 at smoke.brl.mil>, gwyn at smoke.brl.mil (Doug Gwyn) writes:
-|>In article <1391 at gtx.com> randy at gtx.UUCP (Randy D. Miller) writes:
-|>-Can NULL always be cast to a function pointer, even in implementations
-|>-that legally define NULL as (void *)0?  For example, are the following
-|>-lines legal?
-|>-   /* example 1 */     int (*f1)(void) = NULL;
-|>-   /* example 2 */     int (*f2)(void) = (int (*)(void))NULL;
-|>No, the second is not required to be supported by the implementation,
-|>but the first is (3.2.2.3).
-What's wrong with the second example? A null pointer constant 
-represented by "(void *)0" is first cast to a function pointer (perfectly
-legal), and the resulting expression is assigned to the variable
-f2, of identical type, which should also be legal. Can you explain the
-problem?

The problem is that you can't point to any requirement in the standard
that the implementation support casting (void*)0 to a pointer to function.

A null pointer constant can be safely compared to a pointer to function,
and it can be safely assigned to a pointer to function modifiable lvalue,
but that's it.  Casting a null pointer constant to pointer to function is
not permitted in a strictly conforming program.



More information about the Comp.lang.c mailing list