William M. answered 12/21/19
College Professor of C++ with 7+ years of teaching experience
You have three questions here...
1. Why is `typedef` used?
(1) typedefs are used traditionally to simplify complex declarations to be easier to write.
Below is a simple example for structs.
For example, in C and C++ you define structures using the keyword struct.
In C (unlike C++), you *must* also use the struct keyword *every time* you refer to that struct !
But there's a workaround, use a typedef like this...
struct point {
double x;
double y;
};
// in C++ you don't need to write the word struct every time you use the point struct
point pt = { 1, 2 };
// in C (without typedefs), you would have to do so!
struct point pt = { 1, 2 };
// but, typedefs come to the rescue, just add this line...
typedef struct point point;
struct point {
double x;
double y;
};
point pt = { 1, 2 }; // now you use the typedef (point) to mean (struct point)
Part 2 of your question.
2. The syntax looks odd; after `void` should there not be a function name or something? It looks like an anonymous function.
Ok, several issues here.
void (*fp)() is the typical syntax for a function pointer. It tells C/C++ that you have a pointer to executable code, not a pointer to data which is more commonly used in C/C++.
You're probably very comfortable with writing code like...
int x = 10;
int* px = &x; (px is a pointer to int variable x).
printf("*px is: %d\n", *px); // will print: *px = 10
But what if we had code like this:
void hello() {
printf("Hello, world!\n");
}
void goodbye() {
printf("Goodbye, Joe\n");
}
then we could define a function pointer like so:
// define a function pointer named fp
// that will point to a function that returns void and that takes no arguments
void (*fp)(); // define a function pointer variable named fp
fp = hello; // assign fp to point to function hello
fp(); // use fp to call function hello
fp = goodbye;
fp(); // use fp to call function goodbye
You can see a function pointer is NOT an anonymous function (also called a lambda function),
it is a pointer to code, that can be assigned to point to different functions, and can call those functions through the pointer.
So what does this do:
typedef void (*FunctionFunc)(); ?
it makes a typedef called FunctionFunc; it does not define a variable name.
It defines a type, in this case, a function pointer to a function that returns void, and which takes no arguments.
That type will (presumably) be used in the future to create function pointers that point to functions in memory.
How would we use it? Like this:
FunctionFunc fp1 = hello;
FunctionFunc fp2 = goodbye;
fp1(); // calls hello
fp2(); // calls goodbye
Which brings us to your final question?
3. Does typedef void (*FunctionFunc)() create a function ptr variable?
No. It defines a type that will (presumably) be used in the future to create function ptr variables, and that will point to the memory address of a function.
Because you used the keyword typedef, FunctionFunc is NOT the name of a variable, it is the name of a new type.
What if you didn't use the keyword typedef, then it would have been the name of a variable, just like this code we saw earlier
void (*fp)(); // creates a function pointer variable called fp
void (*california)(); // creates a function pointer variable called california;
Hope that cleared up your questions.