I want to assign a function\'s address to a function pointer, but the function to be addressed returns a function pointer with the same signature as itself, causing it to recurs
I suspect what you are trying to do is a more complex version of something like this:
typedef MainLoop *MainLoop(); // not legal
extern MainLoop* main_loop_1();
extern MainLoop* main_loop_2();
MainLoop* main_loop_1()
{
// do some work here
return main_loop_2;
}
MainLoop* main_loop_2()
{
// do some work here
return main_loop_1;
}
int main()
{
MainLoop f = main_loop_1;
for (;;) {
f = f();
}
}
A workaround is to wrap the function pointer in a struct:
struct MainLoop {
MainLoop (*function_ptr)(); // legal
};
extern MainLoop main_loop_1();
extern MainLoop main_loop_2();
MainLoop main_loop_1()
{
// do some work here
return {main_loop_2};
}
MainLoop main_loop_2()
{
// do some work here
return {main_loop_1};
}
int main()
{
MainLoop f{main_loop_1};
for (;;) {
f = f.function_ptr();
}
}