1

(int ( * ) (void*, void*))(numeric ? numcmp : strcmp));

numcmp and strcmp are functions with two arguments.

I understand what the conditional operator is doing. That is straightforward.

I can reason that this will evaluate to numcmp(void*, void*) or strcmp(void*, void*), but I don't understand why? Particularly, the: int (*), confuses me.

Kunseok
  • 263
  • 1
  • 7
  • Possible duplicate of [How do function pointers in C work?](http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work) – Siguza May 20 '17 at 22:51
  • Are you asking why the syntax is the way it is (so awkward)? – Aluan Haddad May 20 '17 at 23:10
  • Side note: calling the resulting pointer is UB. `strcmp`'s parameters aren't `void*`, neither are `numcmp`'s (otherwise the conditional wouldn't compile). Of course, that's exactly the compiler error that you get if you don't shut it up with that cast. – Quentin May 20 '17 at 23:20
  • I didn't know that you could cast something to an entire function including its arguments. Coming from java, we just casted obects/variable types. – Kunseok May 21 '17 at 19:14

1 Answers1

3

The expression (int ( * ) (void*, void*)) is just a cast to a function pointer with two void* arguments returning int. As for other casts, the syntax resembles a variable declaration without variable name. Then depending on the boolean switch, it is decided which of the functions to cast.

nucleon
  • 1,128
  • 1
  • 6
  • 19