Xref: utzoo comp.lang.c:27830 gnu.gcc:1552 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!sharkey!amara!mcdaniel From: mcdaniel@amara.uucp (Tim McDaniel) Newsgroups: comp.lang.c,gnu.gcc Subject: Re: How do I cast to "pointer to function returning int" ? Message-ID: Date: 13 Apr 90 00:16:48 GMT References: <6090.2621f6c2@csv.viccol.edu.au> <90100.172535CMH117@psuvm.psu.edu> Sender: news@amara.UUCP Followup-To: comp.lang.c Organization: Applied Dynamics Int'l. Lines: 42 In-reply-to: CMH117@psuvm.psu.edu's message of 10 Apr 90 21:25:35 GMT CMH117@psuvm.psu.edu (Charles Hannum) writes: Casting a void function to an int function is extremely non-portable. Not quite. ANSI C, and many existing compilers, requires that function pointers all be the same size. Any pointer to a function can be cast into any other pointer-to-function type and back again, producing the same pointer value. However, the value can't be called while it is cast to a different type. For example: typedef anytype (*PF_ANY )(); typedef othertype (*PF_OTHER)(); /* * PF_ANY is the type "pointer to function () returning anytype". * PF_OTHER is the type "pointer to function () returning othertype". * Assume that anytype and othertype are different types. */ PF_ANY f1, f2; PF_OTHER g; ... g = (PF_OTHER) f1; /* legal under ANSI C */ f2 = (PF_ANY) g; /* legal under ANSI C */ if (f1 != f2) { /* then this compiler is not ANSI C compliant. */ } (*f2)(); /* exact same result as "(*f1)();" */ (*g)(); /* illegal under ANSI C */ And, by the way, f2 = (PF_ANY) (void *) f1; is illegal in ANSI C. "void *" is the universal pointer only for DATA objects, not functions. You can convert any FUNCTION pointer to any other FUNCTION pointer type. To rephrase, then: Casting a pointer to void function to a pointer to int function type, and calling that result, is extremely non-portable. -- Tim McDaniel Applied Dynamics International, Ann Arbor, MI Internet: mcdaniel%amara.uucp@mailgw.cc.umich.edu UUCP: {uunet,sharkey}!amara!mcdaniel