function returning pointer to itself
Karl Heuer
karl at haddock.ISC.COM
Fri Jul 15 04:14:22 AEST 1988
In article <11514 at steinmetz.ge.com> davidsen at crdos1.UUCP (bill davidsen) writes:
>The first [of three solutions] may only be used if all procedures are global.
Not true; the same method works with static functions (provided you correctly
forward-declare them as static).
>... method three:
>This is close to what you want, but gives warnings on some compilers.
>I would not use it personally for portability reasons. ...
>The assumption is that a pointer to a function returning any one thing
>looks like a pointer to a function returning some other thing. I can't
>think of any reason this wouldn't be true, but I don't trust it.
The dpANS guarantees that you can cast function pointers to other function
pointer types and back; as long as you use the "right" type to invoke it. I
think there was at least one typo in your code fragment, though. Try this:
typedef void (*func_t)(void); /* arbitrary function pointer type */
func_t do_init(void), do_state_1(void);
func_t (*next_state)(void) = do_init;
next_state = (func_t (*)(void))(*next_state)();
Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
More information about the Comp.std.c
mailing list