1

I am quite new to C and I can not successfully understand the following code:

#include <signal.h> //1
typedef void (*sighandler_t)(int); //2
sighandler_t signal (int signo, sighandler_t handler); //3

Line 3 is:

signal is a function that takes 2 arguments, one being an integer and the other being a sighandler_t and returns a sighandler_t?

But what is sighandler_t?

Is it a pointer to a function, where the function that is being pointed is a function that takes an argument of type int and returns void?

Can you give an example on how I can use it?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • 1
    signal() is depreciated these days. "man signal" says: The behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use:use sigaction(2) instead. – Yetti99 Jun 04 '15 at 20:55
  • 6
    @StarPilot It does, since 16 years ago. – Ivan Aksamentov - Drop Jun 04 '15 at 20:56
  • gcc (at least) recognizes C++ style comments. It is C. – Gil Hamilton Jun 04 '15 at 20:57
  • If you plan to use **function pointers** in `C` consider using **raw pointers** instead. – Alexandre Severino Jun 04 '15 at 20:57
  • @AlexandreSeverino This is from a book really. Linux System Programming. What do you mean use raw pointers? – Koray Tugay Jun 04 '15 at 20:58
  • @AlexandreSeverino; Using function pointer this way becomes easy. – haccks Jun 04 '15 at 20:59
  • No C compiler I ever used understands it. I've used several C++ compilers operating on C code. Tthose compilers know about it. So was the C specification updated to include C++ comments, or are you people just relying on the fact that your C++ compiler recognizes it as a comment and presume that it is standard? – StarPilot Jun 04 '15 at 21:01
  • @haccks, I'm just stating that because the OP is new to `C`. IMO, it would be better if he knew exactly what was going on. Here are some information on **raw function pointers**: http://c.learncodethehardway.org/book/ex18.html – Alexandre Severino Jun 04 '15 at 21:01
  • @StarPilot; C11: **6.4.9** : *Except within a character constant, a string literal, or a comment, the characters `//` introduce a comment that includes all multibyte characters up to, but not including, the next new-line character.* – haccks Jun 04 '15 at 21:03
  • 1
    @hacks, thanks! I appreciate it! – StarPilot Jun 04 '15 at 21:05

1 Answers1

3

Line

typedef void (*sighandler_t)(int);  

define a new type sighandler_t which is pointer to a function which expects an argument of type int and returns nothing.

Line

sighandler_t signal (int signo, sighandler_t handler);  

declares signal a function which expects its first argument of type int and second argument of type sighandler_t and returns a type sighandler_t.
sighandler_t is user defined type which is ultimately pointer to a function which expects an argument of type int and returns nothing.

Without typedef it would be written as

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

which is more confusing.

haccks
  • 104,019
  • 25
  • 176
  • 264