I want to know the benefits of function pointers in computer and embedded systems programming.
-
32. is a function that takes a function pointer, 4. is a function that returns a function pointer – Jul 19 '17 at 12:04
-
We are not a tutoring service. Your question is too broad. A good C book will help. – too honest for this site Jul 19 '17 at 12:46
-
OK, sorry I'm actually got to function pointers is because of the function pointers I found in AVR twi library which wrapped in wire library for Arduino, so those function pointers are still not clear to me and want to understand what are the function callbacks for an interest of event. – R1S8K Jul 19 '17 at 14:03
-
Let me try to change the topic to function callbacks for C library in conjunction with C++ library. – R1S8K Jul 19 '17 at 14:04
3 Answers
- Same as regular typdefs, incredibly useful for 2 and 4. Instead of typing the whole thing, you can now use
myFuncDef
- A function declaration for a function that recieves a function pointer. A function that takes a function pointer of 1. would thus look
int add2to3(myFuncDef functionPtr);
- -
- A function that takes an int and (probably based on that) returns you a function pointer. Using the typedef it would have looked like
myFuncDef functionFactory(int n);
Without typedefs these chains of pointers to pointers could get incredibly long as can be seen in this question: C syntax for functions returning function pointers

- 3,060
- 12
- 28
-
1UV'd, the `typedef` here is indeed good practice to keep this kind of code *somewhat* readable for humans ;) – Jul 19 '17 at 12:08
-
Yes, thank you for the link, the expansion of pointers and the relationship to braces and stars looks simple, but really how that works in real life? That should be awesome to reach a level where to deal with function pointers and see how they are so powerful. – R1S8K Jul 19 '17 at 12:31
1) typdef
of function pointer will help to return a function pointer from a function.
i.e., typedef int (*myFuncDef)(int, int);
myDuncDef retFunc()
here retFunc return the function pointer.
2)int add2to3(int (*functionPtr)(int, int));
here add2to3
function gets the function pointer with specified type as a argument.
3) ------
4) int (*functionFactory(int n))(int, int)
this is another way of returning a function pointer.

- 1,448
- 3
- 16
- 29
typedef(s) are used to simplify declarations.
This declaration
int add2to3(int (*functionPtr)(int, int));
declares a function that accepts as argument a pointer to a function with two parameters of the type int
and has the return type int
.
To make this declaration simpler you can write the parameter as having a type of function instead of the type pointer to function.
int add2to3( int function( int, int ) );
Or you can use the typedef
typedef int (*myFuncDef)(int, int);
//...
int add2to3( myFuncDef functionPtr );
This declaration
int (*functionFactory(int n))(int, int);
declares a function that has one parameter of the type int
and has the return type pointer to a function that has two parameters of the type int
and has the return type int
.
Again using the typedef above you can simplify the declaration
typedef int (*myFuncDef)(int, int);
//...
myFuncDef functionFactory(int n);

- 301,070
- 26
- 186
- 335