I'm learning how to load DLL s dynamically, but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. I would appreciate it if someone could answer them.
- Why use typedef?
- The syntax looks strange; should there be no function name or other name after void? It looks like an anonymous function.
- Is a function pointer created to store the memory address of the function?
So I'm confused now; can you clarify for me?
#1 building
#include <stdio.h> #include <math.h> /* To define a new type name with typedef, follow these steps: 1. Write the statement as if a variable of the desired type were being declared. 2. Where the name of the declared variable would normally appear, substitute the new type name. 3. In front of everything, place the keyword typedef. */ // typedef a primitive data type typedef double distance; // typedef struct typedef struct{ int x; int y; } point; //typedef an array typedef point points[100]; points ps = {0}; // ps is an array of 100 point // typedef a function typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point) // prototype a function distance findDistance(point, point); int main(int argc, char const *argv[]) { // delcare a function pointer distanceFun_p func_p; // initialize the function pointer with a function address func_p = findDistance; // initialize two point variables point p1 = {0,0} , p2 = {1,1}; // call the function through the pointer distance d = func_p(p1,p2); printf("the distance is %f\n", d ); return 0; } distance findDistance(point p1, point p2) { distance xdiff = p1.x - p2.x; distance ydiff = p1.y - p2.y; return sqrt( (xdiff * xdiff) + (ydiff * ydiff) ); }
#2 building
typedef is used for alias types; in this case, you want to alias the functionfunction with void(*) ().
-
Indeed, the syntax looks really strange. Please look at the following:
typedef void (*FunctionFunc) ( ); // ^ ^ ^ // return type type name arguments
-
No, it just tells the compiler that the type of functionfunction will be a function pointer. It does not define a function pointer, as follows:
FunctionFunc x; void doSomething() { printf("Hello there\\n"); } x = &doSomething; x(); //prints "Hello there"
#3 building
If there is no typedef word, in C + +, a functional variable of pointer type of parameterless functional will be declared and void will be returned.
Use typedef to define FunctionFunc as the name of the type.
#4 building
typedef is a language construct that associates names with types.
For example, you can use it in the same way as the original type
typedef int myinteger; typedef char *mystring; typedef void (*myfunc)();
Use them like
myinteger i; // is equivalent to int i; mystring s; // is the same as char *s; myfunc f; // compile equally as void (*f)();
As you can see, you can replace the typedef name with the definition given above.
The difficulty lies in pointers to function syntax and readability in C and C + +, and typedef can improve the readability of such declarations. However, the syntax is appropriate because, unlike other simpler types, functions may have return values and parameters, and therefore sometimes lengthy and complex function pointer declarations.
Readability can start to get tricky with pointers to arrays of functions and other more indirect ways.
Answer your three questions
Why use typedef? Simplify code reading - especially pointers to function or structure names.
The syntax looks strange (in pointers to function declarations), at least at the beginning, and it's not obvious. Use typedef declaration to simplify reading
Is a function pointer created to store the memory address of the function? Yes, the function pointer stores the address of the function. This has nothing to do with a typedef construct that simplifies only the writing / reading of the program; the compiler simply extends the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int); int product(int u, int v) { return u*v; } t_somefunc afunc = &product; ... int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
#5 building
If you can use C ++ 11, you may want to use the std::function and using keywords.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;