Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!gatech!gitpyr!byron From: byron@pyr.gatech.EDU (Byron A Jeff) Newsgroups: comp.lang.c Subject: Re: function casting Message-ID: <8088@pyr.gatech.EDU> Date: 1 May 89 15:31:11 GMT References: <12481@umn-cs.CS.UMN.EDU> Reply-To: byron@pyr.UUCP (Byron A Jeff) Organization: Georgia Institute of Technology Lines: 46 In article <12481@umn-cs.CS.UMN.EDU> clark@umn-cs.CS.UMN.EDU (Robert P. Clark) writes: - -How do I cast something to a pointer to a function that returns -an integer? I'm trying to save the old function, so that I can -call my own function and then chain to the original. - - -One of my (failed) attempts has been - - -main() -{ - int (*f)(); - char *foo(); - - - f = ((*int)())foo(); /* foo returns char*, but I know this is */ - /* really an address of a function */ -} - - - Try this: f = (int (*)()) foo(); Because of the parens around the * it is interpreted first. It reads "cast to a pointer to a function that returns int". A simple rule of thumb I just though of for doing this: Use exactly the same declaration for the variable you're trying to cast to but remove the name (and of course put an extra set of parens around it.) So int (*f)() becomes (int (*)()). Yes? Also it's best to make foo return the same type as f unless there's a real good reason not to (IMHO). BAJ - - Bob Clark clark@umn-cs.cs.umn.edu -- Another random extraction from the mental bit stream of... Byron A. Jeff Georgia Tech, Atlanta GA 30332 Internet: byron@pyr.gatech.edu uucp: ...!gatech!pyr!byron