Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!cmcl2!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: is (int (*)())NULL legal when NULL is (void *)0? Message-ID: <14463@smoke.brl.mil> Date: 15 Nov 90 18:23:49 GMT References: <1391@gtx.com> <14457@smoke.brl.mil> <13799@crdgw1.crd.ge.com> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 23 In article <13799@crdgw1.crd.ge.com> volpe@camelback.crd.ge.com (Christopher R Volpe) writes: -In article <14457@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes: -|>In article <1391@gtx.com> randy@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.