Often I find the need to write functions which return function pointers. Whenever I do, the basic format I use is:
typedef int (*function_type)(int,int);
fu
int (*getFunc())(int, int) { … }
That provides the declaration you requested. Additionally, as ola1olsson notes, it would be good to insert void:
int (*getFunc(void))(int, int) { … }
This says that getFunc may not take any parameters, which can help avoid errors such as somebody inadvertently writing getFunc(x, y) instead of getFunc()(x, y).
You can write the following code(It only works in C++11 and above):
//C++11
auto func(...) {
int (*fptr)(...) ret = ...
//Do sth.
return ret;//C++11 compiler will automatically deduce the return type for you
}
Or, if you do not like automatic return type deduction, you can specified the type at the end of the function(Same as above, only in C++11 and above):
//C++11
auto func(...) -> int (*)(...) { /* Do sth. */ }
You can probably do something like:
int foo (char i) {return i*2;}
int (*return_foo()) (char c)
{
return foo;
}
but god, I hope I'll never have to debug you code....
I think you've got three options:
This is a stupid example, but it's simple and it does not give errors. It's just about declaring static functions:
#include <stdio.h>
#include <stdlib.h>
void * asdf(int);
static int * hjkl(char,float);
main() {
int a = 0;
asdf(a);
}
void * asdf(int a) {return (void *)hjkl; }
static int * hjkl(char a, float b) {int * c; return c;}
ill leave this here since it was a bit trickier than answers already given, as it takes a function pointer
(int (__cdecl *)(const char *))
and returns a function pointer
(int (__cdecl *)(const char *))
#include <stdio.h>
int (*idputs(int (*puts)(const char *)))(const char *) {
return puts;
}
int main(int argc, char **argv)
{
idputs(puts)("Hey!");
return 0;
}