Just to make sure we are on the same page. A typedef is a way of creating a new type that actually refers to an old type. For example the line
typedef int time_t;
creates a new type time_t
that is just an alias for the type int
. This is quite useful for application development because it allows us to encapsulate our types. For example, lets assume now, after 30 years, we realize that the 32-bit int type is no longer large enough to store a time value. With a good architecture, we can change that line to be:
typedef long long int time_t;
And in theory everything will be fine and dandy!
Back to the original question. The line you see is a typedef similar to this, but with some strange syntax. This strange syntax is a way of typedef'ing a function pointer -- what is a function pointer? A function pointer is a way of passing a function around as a value which allows the code to call a function without necessarily knowing exactly what function it is.
Back to the typedef. It is saying "create a new type function_name
such that it is the class of functions that accept a void*
as input and return void". So the new type function_name
is the type given to any function that meet those requirements (return nothing, takes void*
). It lets me write code like this:
typedef void function_name (void* data);
void my_function(void* data) // my_function has type function_name
{
printf("%p\n", data);
}
void invoke_twice(function_name fn, void* data)
{
fn(data); fn(data);
}
int main(int argc, char** argv)
{
invoke_twice(my_function, NULL); // my function _is_ a function
// should print
// 0x0
// 0x0
}
The second statement
static function_name some_func;
Is a little puzzling to me why anyone would do this. It is a very obfuscated way of forward-declaring a function. In other words this statement is equivalent to
void some_func(void* data);
But who am i to judge other code. Without the full context it's hard to extrapolate the intent behind such design decisions.