1

I'm not sure how exactly to read this:

void (*signal(int sig, void (*func)(int))) (int);

I know that void (*func)(int) is a pointer to a function accepting int and returning void.

My best guess is: signal is a function accepting int, and void (*func)(int), which returns a pointer to a function accepting int and returning void (so another void (*func)(int)). I am just a bit confused by this usage, and also the fact that this website is not able to translate it properly.

user129393192
  • 797
  • 1
  • 8

1 Answers1

2

Your reading is correct.

The signal function takes two arguments, an int and a void (*)(int) (i.e. a pointer to a function that takes an int and returns void), and returns a void (*)(int).

As for cdecl, you need to remove the parameter names so it recognizes it as a declaration. So if you pass it this:

void (*signal(int, void (*)(int))) (int);

It returns this:

declare signal as function (int, pointer to function (int) returning void) returning pointer to function (int) returning void

When dealing with function pointers, it makes code more readable to use a typedef for them. In fact, the Linux man page for signal defines it as follows:

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

Which is much clearer than the explicit version.

dbush
  • 205,898
  • 23
  • 218
  • 273