C function question

Can someone explain this line of code.

void ( *signal( int, void (*) (int)) ) ( int );
Last edited on
Break it down from the inside:

void (*) (int)
- This means a pointer to a void function that takes an int as parameter
- To make this easier to understand, we will replace it with a name "sighandler"
typedef void(*sighandler) (int);

*signal( int, void (*) (int) )
- Substitute in our typedef - *signal( int, sighandler *)
- This means a pointer to a "signal" object which takes as parameter an int and a sighandler function pointer

void ( *signal( int, void (*) (int)) ) ( int );
- Again make substitutions - void(*signal(int, sighandler *))(int)
- remember our typedef? Well this function is looking a lot like it, so we do one more substitution...
- More substitutions - sighandler *signal(int, sighandler *)
- And there you have it - a function called "signal" that takes as parameter an int and a sighandler function pointer and returns sighandler function pointer


Of course I didn't know this was a signal handling function until I copied the entire ugliness into google and found this: http://www.docstoc.com/docs/23941968/UNIX-Signals

Want to read more about linux signal handling and also maybe see the above function explained a bit more?
http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/


If this is your first time knowing about pointers to functions (0.o functions can have pointers as well???) here is a place to read more about them :)
http://www.newty.de/fpt/intro.html

I've also made a generic bubblesort function on this site which is in c and takes as parameter a comparison function: http://www.cplusplus.com/forum/general/109081/2/#msg594176
Last edited on
Topic archived. No new replies allowed.