0

Firstly, I am new to C++ on Linux (touching C/C++/Linux after almost 20 years), so apologies for being naive.

I saw the use of signal function in the source for showkey program here and went on searching for how this works. I understood how this works, but it is the declaration of signal function that I am unable to understand.

This Page says that the signal function is defined as

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

In this, I understood

  • return type is void
  • first parameter is int type
  • second parameter is a pointer to function that accepts an int parameter and returns a void

What I am unable to understand is:

  • bracket after void and before star * (in the very beginning) why this ordering of brackets?
  • why signal method name is presented as pointer (*signal(...)?
  • what does the (int) in the end means?

Can anyone please help me understand this. If there is any reference to this advanced level of C pointer usage that will be much beneficial for my overall understanding and learning.

TIA.

Manish Dalal
  • 1,768
  • 1
  • 10
  • 14
  • 1
    Already closed so I can't answer, but use [cdecl.org](https://cdecl.org/?q=void+%28*signal%28int%2C+void+%28*%29%28int%29%29%29%28int%29): "declare signal as function (int, pointer to function (int) returning void) returning pointer to function (int) returning void" (you have to remove the parameter names for it to work) – Kevin Oct 26 '21 at 19:26
  • 2
    The `signal` function returns a pointer to the previous signal handler. In other words the return type of the `signal` function is *"pointer to function that accepts an `int`"*, just like the second argument is a *"pointer to function that accepts an `int`"* So you pass a function pointer in, and you get one back. It's easier to see if you remove the function arguments: `void (*signal(...))(int)` – user3386109 Oct 26 '21 at 19:28

0 Answers0