I want to declare a recursive function type (a function which declares itself) in C.
In a language like Go I can do:
type F func() F
func foo() F {
return foo
}
If I try to do the equivalent in C:
typedef (*F)(F());
I get the following error from GCC:
main.c:1:14: error: unknown type name ‘F’
1 | typedef (*F)(F());
Which makes sense because F doesn't exist at the time of its use. A forward declaration could fix this, how do you forward declare a function type in C?
()
for the argument list), such astypedef int F(int (int (int (int ()))));
. In C, the scope of an identifier begins at the end of its declarator, and function declarators include the(…)
specifying the parameters. And you cannot redeclaretypedef
identifiers with a different type, so you cannot complete it later. – Targumtypedef (*F)(F());
", I think you meantypedef F *F( void );
(function type) ortypedef FPTR (*FPTR)( void );
(function pointer type). But this doesn't change the problem. – Yovonnda