function casting

Byron A Jeff byron at pyr.gatech.EDU
Tue May 2 01:31:11 AEST 1989


In article <12481 at umn-cs.CS.UMN.EDU> clark at 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 at umn-cs.cs.umn.edu
-- 
Another random extraction from the mental bit stream of...
Byron A. Jeff
Georgia Tech, Atlanta GA 30332
Internet:	byron at pyr.gatech.edu  uucp:	...!gatech!pyr!byron



More information about the Comp.lang.c mailing list