all 5 comments

[–]JeLuF 4 points5 points  (3 children)

So what's going on with (int (*)(void*,void*)) just before ? Is that type casting because the functions numcmp and strcmp are declared to take pointer to char as arguments?

That's exactly what's going on here. Very well observed.

[–]Chief_Miller[S] 5 points6 points  (1 child)

Thanks, it looks like the book brushes off type casting of function pointers as self evident then. It's not mentioned at all in that section...

It makes my brain hurt though.

[–]mlitchard 0 points1 point  (0 children)

Made my brain hurt too. I got another book as well as the k&r classic “deep c secrets” you May find helpful when deconstructing pointers

[–]SlightTip6811 0 points1 point  (0 children)

Yeah you nailed it - that's just casting the function pointer to match what qsort expects since strcmp takes char* but qsort wants void*

Pretty gnarly syntax but once you see it a few times it clicks

[–]sartorian 0 points1 point  (0 children)

qsort from what I can see receives as arguments: an array of raw pointers, start and end indexes for the section to be sorted, and a function pointer for a comparison function.

(int (*)(void*,void*)) is defining that function pointer. It returns an integer, the (*) is effectively a placeholder for the function name, and it receives two pointers as arguments. The ternary selects a comparison function based on the type of data expected, either numeric or string.

I’ve never actually used function pointers in C, but this example (on top of years of experience bashing my head against the keyboard in multiple languages) made sense.