0

While studying Signals in Linux, I came across this declaration :

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

The handler function must return void and its prototype has the form :

void my_handler (int signo);

Linux uses a typedef, sighandler_t, to define this prototype.

When I saw Linux Man pages for signal() function it is present there.

But I don't understand What that typedef statement here means.

Can anyone explain what that typedef sighandler_t means?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Badri
  • 13
  • 1
  • 7
  • 1
    It's just giving a name (`sighandler_t`) to the otherwise unnamed (function) type (`void(*)(int)`). Like `typedef struct { double x, y; } point;` gives the name `point` to the untagged (and unnamed) struct. – pmg Nov 12 '19 at 22:13
  • See also [Understanding typedefs for function pointers in C](https://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c). – Jonathan Leffler Nov 13 '19 at 00:28

1 Answers1

1

This is a bit of C magic that says the following:

typedef void (*sighandler_t)(int);

there is a special type called sighandler_t that is a pointer to a function that takes an integer argument and returns nothing.

sighandler_t signal(int signo, sighandler_t handler);

there is a function signal that takes an integer named signo and a function pointer handler as arguments and returns a function pointer.

The typedef literally means "type definition" and is used to define a placeholder for complex types — like function pointers or structures etc.

The definition of a function pointer is a bit beyond the scope of this question, but can be thought of as a command that the user writes, that the framework will call, without knowing exactly what that function will be.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56