Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 (Tek) 9/26/83; site orca.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!zehntel!tektronix!orca!graham From: graham@orca.UUCP (Graham Bromley) Newsgroups: net.lang.c Subject: Re re: declaring routines which return function pointers Message-ID: <1249@orca.UUCP> Date: Mon, 17-Dec-84 12:15:54 EST Article-I.D.: orca.1249 Posted: Mon Dec 17 12:15:54 1984 Date-Received: Wed, 19-Dec-84 00:39:37 EST Organization: Tektronix, Wilsonville OR Lines: 60 > I have a routine which resembles the following: > > int (*f)(); > int fa(), fb(); > > fa() { > f = fb; > return(f); > } > > fb() { > } > > The problem with the above is that lint complains about an > illegal combination of a pointer with an integer in the > 'return' statement. I have tried various casts and function > declarations to try to satisfy lint, but none of them have > worked. Does anybody know what I should do to keep lint happy? > >> I played around with the code for about 30 minutes and the only >> thing I accomplished was to make lint core dump twice. I only >> see one way of doing it and C apparently doesn't support it. >> That way is to declare fa() as: >> >> int ((*fa)())(); >> >> This is broken down from: >> >> int (*x)(); /* x is a pointer to a function returning an int */ >> int ((*y)())(); /* y is function returning a ptr to a func returning an int */ >> >> However, both lint and cc complain with 'function returns illegal >> type'. May I suggest using casts? No no no! The only problem here is incorrect usage of C syntax. You want to declare a function returning a pointer to an integer function. Build up the type declaration as you say it: 1. fa is a function fa() 2. returning a pointer to *fa() 3. an integer int *fa() 4. function int (*fa())() You need the () around *fa() in step 3. because the () operator is evaluated left to right. If this is too confusing (function pointer syntax is rarely crytal clear) use a typedef: typedef int (*IFPTR)(); /* int func ptr */ IFPTR fa(); /* func returning ptr to int func */ To call fa then the function whose address is returned by fa, say: (*fa(a1, a2, ...))(b1, b2, ...) where the a1 etc are fa's arguments, and the b1 etc. are the arguments of the function whose address is returned by fa. Or you could say: int (*f)(); f = fa(a1, a2, ...); (*f)(b1, b2, ...); if you want to remember the function pointer returned by fa. gbgb, aka the longjmp artist (an unfair alias)