Function Returning Itself

Is it possible to declare some function type func_t which returns that type, func_t?

In other words, is it possible for a function to return itself?

// func_t is declared as some sort of function pointer
func_t foo(void *arg)
{
  return &foo;
}

Or would I have to use void * and typecasting?


Solution 1:

No, you cannot declare recursive function types in C. Except inside a structure (or an union), it's not possible to declare a recursive type in C.

Now for the void * solution, void * is only guaranteed to hold pointers to objects and not pointers to functions. Being able to convert function pointers and void * is available only as an extension.

Solution 2:

A possible solution with structs:

struct func_wrap
{
    struct func_wrap (*func)(void);
};

struct func_wrap func_test(void)
{
    struct func_wrap self;

    self.func = func_test;
    return self;
}

Compiling with gcc -Wall gave no warnings, but I'm not sure if this is 100% portable.