I'm trying to make a function that takes a character, then returns a pointer to a function depending on what the character was. I just am not sure how to make a function return a pointer to a function.
-
Will the signature of the returned function always be the same? – sean e Jun 15 '09 at 19:12
-
Here's the signature of a function that takes a function pointer and returns one: http://en.cppreference.com/w/c/program/signal – Felix Dombek Nov 23 '12 at 11:56
14 Answers
int f(char) {
return 0;
}
int (*return_f())(char) {
return f;
}
No, seriously, use a typedef :)

- 33,800
- 13
- 85
- 120
-
@Axalo The program compiles as is. ```return_f``` does not take any arguments but returns a function that takes a char. – erikkallen Apr 24 '15 at 09:21
-
6
-
2provide a shortcut `typedef int (*ifc)(char);` and use it as the return type in your function: `ifc return_f();` – Ralph Aug 25 '19 at 13:22
#include <iostream>
using namespace std;
int f1() {
return 1;
}
int f2() {
return 2;
}
typedef int (*fptr)();
fptr f( char c ) {
if ( c == '1' ) {
return f1;
}
else {
return f2;
}
}
int main() {
char c = '1';
fptr fp = f( c );
cout << fp() << endl;
}
-
2I am wondering why this works. Shouldn't we deference the function pointer first, i.e. `cout << (*fp)() << endl;` ? – qed Mar 12 '14 at 10:21
-
4@qed: No. You can dereference it, but it's perfectly ok not to do it. Usual function are already pointer internally anyway. – xryl669 May 09 '14 at 17:38
-
To @qed and anyone else who stumbles upon this and has the same question: I had a similar question. See the summary of the answer in the bottom of my question here: [C++ Function call via an object with public member pointer to function, without using dereference operator](https://stackoverflow.com/q/31869026/4561887). Whether you do `myFuncPtr();`, `(*myFuncPtr)();`, `(**myFuncPtr)();`, or even `(**********************************f)();`, it makes no difference. They are all valid function calls. – Gabriel Staples Feb 05 '22 at 04:53
Create a typedef for the function signature:
typedef void (* FuncSig)(int param);
Then declare your function as returning FuncSig:
FuncSig GetFunction();

- 11,792
- 3
- 44
- 56
Assuming int f(char)
and ret_f
which returns &f
.
C++98/C++03 compatible ways:
Ugly way:
int (*ret_f()) (char) { return &f; }
With typedef:
typedef int (sig)(char); sig* ret_f() { return &f; }
or:
typedef int (*sig_ptr)(char); sig_ptr ret_f() { return &f; }
Since C++11, in addition we have:
with
decltype
:decltype(&f) ret_f() { return &f; }
trailing return type:
auto ret_f() -> int(*)(char) { return &f; }
or:
auto ret_f() -> decltype(&f) { return &f; }
typedef
withusing
:using sig = int(char); sig* ret_f() { return &f; }
or:
using sig_ptr = int (*)(char); sig_ptr ret_f() { return &f; }
C++14 adds:
auto
deduction:auto ret_f() { return &f; }

- 203,559
- 14
- 181
- 302
In C++11 you can use trailing return types to simplify the syntax, e.g. assuming a function:
int c(int d) { return d * 2; }
This can be returned from a function (that takes a double to show that):
int (*foo(double e))(int)
{
e;
return c;
}
Using a trailing return type, this becomes a bit easier to read:
auto foo2(double e) -> int(*)(int)
{
e;
return c;
}

- 911
- 9
- 7
Here is how to do it without using a typedef:
int c(){ return 0; }
int (* foo (void))(){ //compiles
return c;
}

- 61
- 1
- 1
Syntax for returning the function:
return_type_of_returning_function (*function_name_which_returns_function)(actual_function_parameters) (returning_function_parameters)
Eg: Consider the function that need to be returned as follows,
void* (iNeedToBeReturend)(double iNeedToBeReturend_par)
{
}
Now the iNeedToBeReturend function can be returned as
void* (*iAmGoingToReturn(int iAmGoingToReturn_par))(double)
{
return iNeedToBeReturend;
}
I Felt very bad to learn this concept after 3 years of professional programming life.
Bonus for you waiting down for dereferencing function pointer.
Example for function which returns the function pointer is dlopen in dynamic library in c++

- 1,775
- 4
- 23
- 38
typedef void (*voidFn)();
void foo()
{
}
voidFn goo(char c)
{
if (c == 'f') {
return foo;
}
else {
//..
}
// ..
}

- 127,556
- 20
- 111
- 121
Check out this site - http://cdecl.org
Helps you convert english to C declarations and back!
Cool Stuff!
This link decodes the example in erikallen's answer. int (*return_f())(char)

- 16,443
- 1
- 26
- 26
This is the code to show return of a function pointer. You need to define the "function signature" to return first:
int returnsOne() {
return 1;
}
typedef int(*fp)();
fp returnsFPtoReturnsOne() {
&returnsOne;
}
In your specific case:
fp getFunctionFor(char code) {
switch (code) {
case 'a': return &functionA;
case 'b': return &functionB;
}
return NULL;
}

- 727
- 8
- 19
Easiest way is to typedef the pointer-to-function type you want, and then use that
typedef void (*fnptr_t)(int, int);
fptr_t myfunc(char *) { ....

- 2,920
- 15
- 10
I prefer returning objects and call the operator(). This way your function can return an interface and all classes can inherit from this. That is, if you're using C++ and not C.
Then you can use the parametrized factor method to return the objects based on your input.

- 14,072
- 2
- 31
- 53
I'm assuming C here (no objects) :) :
// Type of function which takes a char and returns an int:
typedef int (*Func)(char a);
// An example of the function you're trying to return and which does something
// with char:
int exampleFunc(char a)
{
return (int)(a + 42);
}
// The function returning the pointer to a function:
Func *returnAfunc(void)
{
return exampleFunc;
}

- 3
- 2

- 4,861
- 1
- 21
- 24
Something like this
#include <iostream>
typedef char (*fn_ptr_t)(char);
char a_fn(char c)
{
return c + 1;
}
char b_fn(char c)
{
return c + 2;
}
fn_ptr_t
return_function(char c)
{
fn_ptr_t result = 0;
switch (c)
{
case 'a':
result = a_fn;
break;
case 'b':
result = b_fn;
break;
}
return result;
}
int
main()
{
fn_ptr_t fn = return_function('a');
std::cout << "a(l) = " << (fn)('l') << std::endl;
return 0;
}

- 7,551
- 3
- 24
- 27