What is it?
typedef void (*print_type) (const char*);
The typedef tells the compiler that the data type print_type
is a pointer to a function which accepts const char*
as an argument and does not return a value.
... and it has nothing to do with a DLL
Function Pointers
We declare a function as follows:
ret-type identifier(parameters)
int f(double x)
Given that for something declared as int a
, the pointer to it is declared as int *a
(adding a * after the data type), it is natural for someone to come up with a way to declare a function pointer as follows:
int *f(double);
There's a catch here. That is actually a declaration of a function which accepts a double
as an argument and returns a int*
.
We need to explicitly tell the compiler that it is not a function returning a pointer; rather it is a pointer to a function. We do this by placing brackets (parenthesis has higher precedence) around *f
.
int (*f)(double);
Let's take another example:
int* myfunc(int a[], char b);
To declare an array of function pointers which point to a function with the above signature, we write:
int* (*ptr[])(int[], char);
There is a trick known as Spiral Rule which is quite useful in finding out what a complicated data type is.
Using typedef
Things can get pretty complicated with function pointers. To keep the code readable and neat, we prefer giving a single symbol type name to function pointer types using typedef.
typedef int (*ptrFunc_t)(int, char);
This allows you to declare a pointer to a function which accepts an int
and a char
as arguments and returns an int
as follows:
ptrFunc_t myptr;