Possible Duplicate:
What does “void *(*)(void *)” mean in c++?
What does the type void(*)(void *) mean?
I came across this type in the example code for the book "Mastering Algorithms with C"
void list_init(List *list, void (*destroy)(void *data))
{
...
...
}
The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
Void as a Function Return TypeThe void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement. For example, a function that prints a message doesn't return a value. The code in C++ takes the form: void printmessage ( )
A void pointer in C++ is a special pointer that can point to objects of any data type. In other words, a void pointer is a general purpose pointer that can store the address of any data type, and it can be typecasted to any type. A void pointer is not associated with any particular data type.
The (void) indicates the return type. This method doesn't return anything, so its result can't be assigned to anything.
It's a function pointer.
void (*destroy)(void *data)
destroy
is a pointer to a function which returns void
and takes a void*
as an argument.
cdecl.org is a useful tool for discerning complex C declarations. Also, take a look at the spiral rule.
In this specific case, its a pointer to which any function can be cast to void(*)(void *) and the function parameter void * can be any type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With