How do I cast to "pointer to function returning int" ?

Martin Weitzel martin at mwtech.UUCP
Wed Apr 11 22:05:28 AEST 1990


In article <18834 at duke.cs.duke.edu> drh at romeo.UUCP (D. Richard Hipp) writes:
:In article <6090.2621f6c2 at csv.viccol.edu.au> timcc at csv.viccol.edu.au writes:
:>struct foo {
:>   char *name ;
:>   int (*function) () ;
:>   } ;
:>
:>struct foo foo_table[] = {
:>   "bar",	(int * ()) bar,
:>   "blurfl",	(int * ()) blurfl,
:>   "frobjitz",	(int * ()) frobjitz,
:>   } ;
:
:The typecast (int *()) is for "function returning pointer to integer".
:To get "pointer to function returning integer" use (int (*)()).
:
:The rule is this: operators on the right hand side bind tighter than
:operators on the left hand side.  Thus "()" binds tighter than "*".
:You want the "*" to bind closest, so the extra parenthesis are required.

Or, if you want an easy 'step-to-step' procedure, do it as follows:

	1) write a declaration for some object 'foo', so that
	   'foo' has the desired type.
	2) avoid all unnecessary brackets (here the above
	   rule comes into play, that '()' and '[]' on the right
	   bind more tightly than '*' on the left; use brackets
	   like in expressions, if you want other binding)
	3) cancel the name 'foo' and enclose what remains
	   in brackets
	4) after half an hour take the freshly baked cast
	   out of the oven and enjoy!
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list