-3

In signal.h there is declaration of signal function:

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

How to interpret this and what's the use of declaring in this weird way?

0x90
  • 39,472
  • 36
  • 165
  • 245
mohit
  • 5,696
  • 3
  • 24
  • 37
  • 1
    People, don't upvote this. Dupe hundreds of times. Just becuase you don't understand it, it doesn't mean it's a great question. In fact, it isn't. –  Jun 22 '13 at 05:43
  • @H2CO3 - except that if you paste that into cdecl.org it claims it's a syntax error, even though it isn't. – detly Jun 22 '13 at 05:53
  • @detly Then you paste it with argument names taken out, and it will happily recognize it. –  Jun 22 '13 at 05:54
  • @H2CO3 - Okay, but a beginner wouldn't necessarily know to do that. I didn't. And cdecl is still just plain wrong, because it's not a syntax error. I'm not disputing that this is an FAQ, but cdecl is a useless thing to post here unless you're going to explain how to use it. – detly Jun 22 '13 at 05:58

3 Answers3

3

The signal function takes an int and a function pointer as arguments, and returns a function pointer. The function pointer argument and the returned function pointer each take an int argument, and returns void.

The signal prototype is sometimes written this way:

typedef void (*signal_handler_type) (int);

signal_handler_type signal (int, signal_handler_type);

Since the signal function allows the caller to replace the existing signal handler, it returns the one that was replaced after the call.

jxh
  • 69,070
  • 8
  • 110
  • 193
1

on APUE,

The prototype for the signal function states that the function requires two arguments and returns a pointer to a function that returns nothing (void ). The signal function's first argument, signo , is an integer. The second argument is a pointer to a function that takes a single integer argument and returns nothing. The function whose address is returned as the value of signal takes a single integer argument (the final (int) ). In plain English, this declaration says that the signal handler is passed a single integer argument (the signal number) and that it returns nothing. When we call signal to establish the signal handler, the second argument is a pointer to the function. The return value from signal is the pointer to the previous signal handler.

The perplexing signal function prototype shown can be made much simpler through the use of the following typedef:

typedef void Sigfunc(int);

Then the prototype becomes

Sigfunc *signal(int, Sigfunc *)
vvy
  • 1,963
  • 13
  • 17
0

Signal function specifies a way to handle the signals with the signal number specified by signo(here).

Parameter func specifies one of the three ways in which a signal can be handled by a program: you can look [here][1]

[1]: http://www.cplusplus.com/reference/csignal/signal/ for more details

0decimal0
  • 3,884
  • 2
  • 24
  • 39