Reputation: 518
We came across this snippet of code in our textbook that was lacking explanation except the fact that it handles signals.
#include <signal.h>
void (*signal(int signr,
void(*sighandler)(int)
)
)(int)
I know that sighandler
is a pointer to a function, but I do not understand if it is actually executed or just returned?
and what does the call with (int)
do?
It looks almost like a reversed cast.
Upvotes: 2
Views: 114
Reputation: 224062
This is the signature of the signal
function.
The first argument signr
is of type int
and is the signal whose handler you want to change.
The second argument sighandler
is a function pointer of type void (*)(int)
, i.e. a function that takes an int
and returns void
. This parameter is the name of a function which will handle the signal.
The function returns a function pointer of type void (*)(int)
(same type as argument 2) which points to the previous signal handling function.
Breaking it down:
signal // signal
signal() // is a function
signal(int) // taking a int
signal(int, void (*)(int)) // and a function pointer
// which takes an int and return void
(*signal)(int, void (*)(int))(int) // and returns a function pointer
// which takes an int
void (*signal)(int, void (*)(int))(int) // and returns void
The man page includes a typedef which makes this a bit more clear:
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
Upvotes: 6