Re re: declaring routines which return function pointers
Graham Bromley
graham at orca.UUCP
Tue Dec 18 03:15:54 AEST 1984
> 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)
More information about the Comp.lang.c
mailing list