turning typedef use into an inline expression: SUMMARY REPLY

Michael Ryan ryan at arisia.Xerox.COM
Thu Dec 13 12:45:35 AEST 1990


In article <14861 at arisia.Xerox.COM> ryan at arisia.Xerox.COM (Michael Ryan) writes:
>I am trying to figure how to change the 'pointer to function returning int'
>type from a typedef into an inline definition.  the usage would be for a 
>function that returns a pointer to a function returning an int.
>
>e.g:
>/* in file main.c */
>typedef int (*pfi)();
>extern pfi dave();

thanks to dkeisen at gang-of-four.stanford.edu, karl at ima.isc.com,
pds%lemming.webo.dg.com at relay.cs.net, fwa at gupta.portal.com, 
and jerbil at cobalt.cco.caletch.edu.

first, the type:

a function that returns a pointer to a function that returns an int ==  
	int (*dave())()

next, the method:

0.  start by keeping some things in mind:
      a. start with the actual thing being defined.  in this case 'dave.'
      b. "postfix modifiers have higher precedence that prefix modifiers;
         parenthesis can be used to override this."
         e.g. '()' has higher precedence than '*.' 
         in our case this means we'll need parenthesis to bind a '*' to 
         dave because we follow it immediately with a '().' (step 3)
      c. function have arguments.  denoted here by '...'
1.  'dave' is a function:	dave(..)
2.  that returns a pointer:	*dave(...)
3.  to a function:		(*dave(...))()
4.  that returns an int:	int (*dave(...))()
	
NOTE: in step 3 without the parenthesis the compiler would bind the 
expression so (cdecl):
  declare dave as function returning function returning pointer to int

we can then use this definition so:

int fred() { return 2; };
int (*dave())() { return fred;};

one thing, this was meant as an exercise not as a production item.
as one of the respondents pointed out, using the typedef is much more clear
and should be used in production-level code.  ( whew!)

thanks again
//michael j ryan
-- 
	michael ryan
	ryan at arisia.Xerox.COM
	""



More information about the Comp.lang.c mailing list