After figuring out the signature of the signal
function, I modified the example given by https://en.cppreference.com/w/c/program/signal.
But why can't I call the function (the signal handler) returned by the signal
, instead I can call it direclty ?
void (*signal( int sig, void (*handler) (int))) (int);
The signal
function returns a pointer to function, which is void (*)(int)
.
Return value
Previous signal handler on success or
SIG_ERR
on failure (setting a signal handler can be disabled on some implementations).
#include <signal.h>
#include <stdio.h>
void signal_handler(int signal)
{
printf("hahahah\n");
}
int main(void)
{
void (*f1)(int);
f1 = signal(SIGINT, signal_handler);
f1(3); //Get signal SIGSEGV and failed
// signal_handler(3); //OK
raise(SIGINT);
}
I know it might look like a meaningless question, but the point is, f1
points to signal_handler
, so calling f1(3)
is just like calling signal_handler(3)
, I don't understand why the latter is ok but not the former, there should be not "tricks" that can make between these two calling function statments, I think.