Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!mcdphx!hrc!gtx!randy From: randy@gtx.com (Randy D. Miller) Newsgroups: comp.lang.c Subject: is (int (*)())NULL legal when NULL is (void *)0? Message-ID: <1391@gtx.com> Date: 12 Nov 90 22:46:45 GMT Reply-To: randy@gtx.UUCP (Randy D. Miller) Organization: GTX Corporation, Phoenix, Arizona Lines: 47 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; According to ANSI X3.159-1989 3.2.2.3 (pointer conversions) and 3.3.16.1 (assignment operator), example #1 is clearly legal even when NULL is (void *)0, because the null pointer constant is a special case (but no other pointer to void may be converted to a pointer to a function). But is the cast in example #2 legal? It just does explicitly what is done implicitly in example #1. I tried it on a few compilers and lints that define NULL as (void *)0; only Microsoft version 6.0 rejected it with an "illegal cast" error message (using cl -W4 -Za). Chris Torek, in <15047@mimsy.UUCP> said, >(remember that a cast is equivalent to assignment to an unnamed >temporary variable---I like to consider `assignment' to include all >casts). If that is true, then example #2 must also be legal. However, I can't see anything in the Standard that specifies that all the constraints of section 3.3.16.1 (assignment operator) apply to section 3.3.4 (cast operator). 3.3.16.1 specifically allows that a null pointer constant may be assigned to "a pointer," which includes function pointers. 3.3.4 doesn't specifically treat casts of the null pointer constant. But maybe this wording in 3.3.4 associates the two sections: "Conversions that involve pointers (other than as permitted by the constraints of 3.3.16.1) shall be specified by means of an explicit cast;" But surely that can't mean that if a conversion is "permitted" by 3.3.16.1, it *cannot* have an explicit cast, can it? If so, then the cast operator is unlike an assignment in this one special case where the null pointer constant is converted to a function pointer, and you must only make the assignment *without* an explicit cast. Are there any other cases where an implicit conversion is legal, but the identical explicit cast is not? Or is Chris Torek right and MSC 6.0 wrong? -- Randy D. Miller sun!sunburn!gtx!randy or hrc!gtx!randy or randy@gtx.UUCP GTX Corp., 8836 N. 23rd Avenue, Phoenix, Arizona 85021 (602) 870-1696