Gum Bi
Gum Bi

Reputation: 323

C - Calling a function declared with parameters with no parameters?

I'm trying to understand a code which have the following lines:

void terminate_pipe(int);
code code code...
struct sigaction new_Sigiterm;
new_Sigiterm.sa_handler = terminate_pipe;

My question are:

thanks.

Upvotes: 0

Views: 109

Answers (3)

TakUnderhand
TakUnderhand

Reputation: 111

Code like this assignment is setting a handler (sometimes called a function pointer): Basically, the address of the function to run, at a given time.

The syntax for this in C is to name the function, but don't put () on the end. That returns the address of the function.

new_Sigiterm.sa_handler = terminate_pipe;

Upvotes: 1

Tom Tsagkatos
Tom Tsagkatos

Reputation: 1494

new_Sigiterm.sa_handler is most likely a pointer that points to a function. By running

new_Sigiterm.sa_handler = terminate_pipe;

It's similar to saying

new_Sigiterm.sa_handler = &terminate_pipe;

(Like in pointers). This is not running the function, it's just making a pointer that points to the function, if you "run" the pointer, the pointed function will run.

This is how to declare function pointer:

void function(int x);

int main()
{
    //Pointer to function
    void (*foo) (int);

    //Point it to our function
    foo = function;

    //Run our pointed function
    foo(5);
}

More info about function pointers

Upvotes: 3

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

  1. void terminate_pipe(int); is not calling of function it is Forward declaration of function.
  2. In new_Sigiterm.sa_handler sa_handler is a Function Pointer.

Upvotes: 0

Related Questions