Unable to understand signal function decleration - Unix

This is a discussion on Unable to understand signal function decleration - Unix ; #include void (*signal(int signo, void (*func)(int)))(int); I am unable to understand that what is the last (int) representing. What this function is returning (of what type). And how above function can be redefined as this: typedef void Sigfunc(int); Sigfunc *signal(int, ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Unable to understand signal function decleration

  1. Unable to understand signal function decleration

    #include



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


    I am unable to understand that what is the last (int) representing.
    What this function is returning (of what type).


    And how above function can be redefined as this:

    typedef void Sigfunc(int);
    Sigfunc *signal(int, Sigfunc *);

    -Sanchit





  2. Re: Unable to understand signal function decleration

    On Mar 20, 7:10 am, Sanchit wrote:
    > #include
    >
    > void (*signal(int signo, void (*func)(int)))(int);
    >
    > I am unable to understand that what is the last (int) representing.
    > What this function is returning (of what type).
    >
    > And how above function can be redefined as this:
    >
    > typedef void Sigfunc(int);
    > Sigfunc *signal(int, Sigfunc *);
    >
    > -Sanchit



    Just use the easier typedef definition (its much easier to read) but
    they are exactly the same.

    The function signal() takes 2 arguments:
    1) int signo: The signal you are installing the handler on.
    2) sig_t handler: A pointer to a function that will handle the
    signal.
    It returns a pointer to the previously installed handler.

    sig_t: Is a pointer to a function that takes one parameter:
    1) int signo: The signal no. (So you can install the same
    handler on multiple signals and differentiate)
    It returns nothing.

  3. Re: Unable to understand signal function decleration

    Sanchit writes:

    > #include
    >
    > void (*signal(int signo, void (*func)(int)))(int);
    >
    > I am unable to understand that what is the last (int) representing.
    > What this function is returning (of what type).


    A function returning a pointer to a function has the type of that
    returned function pointer "wrapped round" it: the type on the left is
    the return type of the returned function pointer (not of the function
    we are defining) and the bracketed parameters on the right are the
    parameters of this returned function pointer type:

    (*f( )) (

    )

    So, ignoring the parameters of f for now, we see that f returns a
    pointer to a function taking parameters P and returning R.

    so, writing signal in this form:

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

    we see it should be read as:

    void (*signal( )) (int);

    i.e. that signal returns a pointer to a function that takes a single
    int and which return void.

    > And how above function can be redefined as this:
    >
    > typedef void Sigfunc(int);
    > Sigfunc *signal(int, Sigfunc *);


    After the typedef, Sigfunc is simply a name for the type "function
    which takes an int and returns void". Hence a function that returns a
    "Sigfunc *" is one that returns a pointer to such a function. That is
    exactly what signal does.

    Finally, note that the second parameter is a pointer to exactly the
    same kind of function, so the second parameter can also be a "Sigfunc *".

    --
    Ben.


  4. Re: Unable to understand signal function decleration

    Thanks a lot Ben and Martin!!!!!!!

    Your explanations cleared my doubt!

  5. Re: Unable to understand signal function decleration

    On Mar 20, 7:57 pm, Ben Bacarisse wrote:
    > Sanchit writes:
    > > #include

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

    >
    > > I am unable to understand that what is the last (int) representing.
    > > What this function is returning (of what type).

    >
    > A function returning a pointer to a function has the type of that
    > returned function pointer "wrapped round" it: the type on the left is
    > the return type of the returned function pointer (not of the function
    > we are defining) and the bracketed parameters on the right are the
    > parameters of this returned function pointer type:
    >
    > (*f( )) (

    )
    >
    > So, ignoring the parameters of f for now, we see that f returns a
    > pointer to a function taking parameters P and returning R.
    >
    > so, writing signal in this form:
    >
    > void (*signal( int signo, void (*func)(int) )) (int);
    >
    > we see it should be read as:
    >
    > void (*signal( )) (int);
    >
    > i.e. that signal returns a pointer to a function that takes a single
    > int and which return void.
    >
    > > And how above function can be redefined as this:

    >
    > > typedef void Sigfunc(int);
    > > Sigfunc *signal(int, Sigfunc *);

    >
    > After the typedef, Sigfunc is simply a name for the type "function
    > which takes an int and returns void". Hence a function that returns a
    > "Sigfunc *" is one that returns a pointer to such a function. That is
    > exactly what signal does.
    >
    > Finally, note that the second parameter is a pointer to exactly the
    > same kind of function, so the second parameter can also be a "Sigfunc *".
    >
    > --
    > Ben.


    You said that P is the parameter then what are here?


  6. Re: Unable to understand signal function decleration

    On Mar 20, 8:44 am, Sanchit wrote:

    > > void (*signal( )) (int);


    > You said that P is the parameter then what are here?


    The 'some parameters' are the parameters the 'signal' function takes.
    If 'signal' takes a void, it would be:

    void (*signal(void))(int);
    and you would call it like this:
    signal();

    if 'signal' took a float, it would be:
    void (*signal(float))(int);
    and you could call it like this:
    signal(3.2);

    So the thing inside those parenthesis are the parameters 'signal'
    takes.

    The 'void' on the left mean that signal returns a function that
    returns void. The 'int' on the right means that signal returns a
    function that takes an integer.

    So, putting it all together:
    void (*signal(int signo, void (*func)(int)))(int);
    A B C D E F G H I J K
    Means signal(C) is a pointer(B) to a function.
    The function signal points to takes an integer(D) called signo(A) and
    a pointer(G) to a function that takes a integer(I) and returns a
    void(F).
    The function signal points to returns a function (J) that takes an
    integer(K) and returns void(A).

    These types of definitions are *very* hard to read, and should
    definitely be broken up. This is *much* better:

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

    This says a 'sighandler_t' is a pointer to a function that takes an
    integer and returns void. The signal function takes an integer and a
    sighandler_t and returns a sighandler_t.

    Much better.

    DS

  7. Re: Unable to understand signal function decleration

    On Mar 20, 10:11 pm, David Schwartz wrote:
    > On Mar 20, 8:44 am, Sanchit wrote:
    >
    > > > void (*signal( )) (int);

    > > You said that P is the parameter then what are here?

    >
    > The 'some parameters' are the parameters the 'signal' function takes.
    > If 'signal' takes a void, it would be:
    >
    > void (*signal(void))(int);
    > and you would call it like this:
    > signal();
    >
    > if 'signal' took a float, it would be:
    > void (*signal(float))(int);
    > and you could call it like this:
    > signal(3.2);
    >
    > So the thing inside those parenthesis are the parameters 'signal'
    > takes.
    >
    > The 'void' on the left mean that signal returns a function that
    > returns void. The 'int' on the right means that signal returns a
    > function that takes an integer.
    >
    > So, putting it all together:
    > void (*signal(int signo, void (*func)(int)))(int);
    > A B C D E F G H I J K
    > Means signal(C) is a pointer(B) to a function.
    > The function signal points to takes an integer(D) called signo(A) and
    > a pointer(G) to a function that takes a integer(I) and returns a
    > void(F).
    > The function signal points to returns a function (J) that takes an
    > integer(K) and returns void(A).
    >
    > These types of definitions are *very* hard to read, and should
    > definitely be broken up. This is *much* better:
    >
    > typedef void (*sighandler_t)(int);
    > sighandler_t signal(int signum, sighandler_t handler);
    >
    > This says a 'sighandler_t' is a pointer to a function that takes an
    > integer and returns void. The signal function takes an integer and a
    > sighandler_t and returns a sighandler_t.
    >
    > Much better.
    >
    > DS

    Thanks David

  8. Re: Unable to understand signal function decleration

    On Mar 20, 11:44 am, Sanchit wrote:
    > On Mar 20, 7:57 pm, Ben Bacarisse wrote:
    >
    >
    >
    > > Sanchit writes:
    > > > #include

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

    >
    > > > I am unable to understand that what is the last (int) representing.
    > > > What this function is returning (of what type).

    >
    > > A function returning a pointer to a function has the type of that
    > > returned function pointer "wrapped round" it: the type on the left is
    > > the return type of the returned function pointer (not of the function
    > > we are defining) and the bracketed parameters on the right are the
    > > parameters of this returned function pointer type:

    >
    > > (*f( )) (

    )
    >
    > > So, ignoring the parameters of f for now, we see that f returns a
    > > pointer to a function taking parameters P and returning R.

    >
    > > so, writing signal in this form:

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

    >
    > > we see it should be read as:

    >
    > > void (*signal( )) (int);

    >
    > > i.e. that signal returns a pointer to a function that takes a single
    > > int and which return void.

    >
    > > > And how above function can be redefined as this:

    >
    > > > typedef void Sigfunc(int);
    > > > Sigfunc *signal(int, Sigfunc *);

    >
    > > After the typedef, Sigfunc is simply a name for the type "function
    > > which takes an int and returns void". Hence a function that returns a
    > > "Sigfunc *" is one that returns a pointer to such a function. That is
    > > exactly what signal does.

    >
    > > Finally, note that the second parameter is a pointer to exactly the
    > > same kind of function, so the second parameter can also be a "Sigfunc *".

    >
    > > --
    > > Ben.

    >
    > You said that P is the parameter then what are here?


    Read this:

    http://www.ericgiguere.com/articles/...l?noprint=true

    Following the rules there, including the ANSI additions:

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

    Gives: declare signal as function (with first parameter of type int
    and second parameter of type pointer to function (with first parameter
    of type int) returning void) returning pointer to function (with first
    parameter of type int) returning void.

    In other words, signal is a function with:
    * 1st parameter of type int
    * 2nd parameter of type pointer to function (a function with param
    of type int, returning void)
    And signal returns a pointer to a function, which also has parameter
    of type int, and returns void.


    Jason


    P.S. Sorry if this is a double post, Google Groups is getting a little
    strange on me.


+ Reply to Thread