7

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)) 
{
...

...
}
Community
  • 1
  • 1
DeepBlack
  • 280
  • 4
  • 10
  • 6
    a function pointer to a function that takes a void pointer as parameter – TJD Oct 11 '12 at 01:22
  • 4
    Spiral rule: *pointer to a function taking a pointer to void and returning nothing*. – chris Oct 11 '12 at 01:23
  • 2
    By the way, if you're using C++11, this becomes `std::function`, which is much more readable and useful. While doing that, you could replace the `void *` with a template if you're after any type. – chris Oct 11 '12 at 01:26

2 Answers2

11

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.

Community
  • 1
  • 1
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Thanks for the responses. I just figured that the author was using this function pointer to assign the function 'free(void *)' (stdlib.h) to the function pointer 'destroy'. Not the most readable way to do it tough. Does anyone know of a good reason this might be useful, in spite of readability issues? I'm not sure if my comment here is clear enough. I don't know if I could post the entire code here (copyrights). – DeepBlack Oct 11 '12 at 01:49
  • 1
    @user1535080 `destroy` is a parameter, and `free` isn't the only possible argument. " Not the most readable way to do it tough" -- what would you do instead? "Does anyone know of a good reason this might be useful" -- yes; to pass an arbitrary "destroy" function to `list_init`. – Jim Balter Oct 11 '12 at 02:30
  • @user1535080: Consider a type which allows for customized deallocation routines. The function prototype is one that returns `void` and takes a `void*` as a parameter, but perhaps you want to do the deallocation yourself (a self-managed heap) or just perform some logging. This lets you assign any function with a specific signature to handle the deallocation. I realize that the function pointer syntax seems strange at first, but this is C, and after you get some experience you won't think it is unreadable (plus, as Jim said, how else would you do it?) – Ed S. Oct 11 '12 at 02:41
  • For which compilers is this syntax legal? The R package igraph does not compile on the CRAN Solaris server due to a "syntax error" around the use of "void (*)(void)": https://www.r-project.org/nosvn/R.check/r-patched-solaris-x86/igraph-00install.html – landau Jul 20 '17 at 15:57
  • 1
    could you also write it without the parenthesis as `void* destroy(void *data)` ? – Jorge Luque Nov 20 '20 at 23:33
  • or I guess that would make it a function that returns a void pointer and not a pointer to a void function? – Jorge Luque Nov 20 '20 at 23:41
  • 1
    @JorgeLuque: Yes, that would be incorrect – Ed S. Dec 02 '20 at 22:57
3

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.

bobestm
  • 1,334
  • 1
  • 9
  • 8