How to interpret this declaration

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jul 15, 2005.

  1. Guest

    I found the following declaration in some header file.
    Can somebody help me how to interpret this ?

    void (*signal( int sig, void (*function)(int)) (int) );


    Thanx for any help in advance ....
    , Jul 15, 2005
    #1
    1. Advertising

  2. S.Tobias Guest

    wrote:
    > I found the following declaration in some header file.
    > Can somebody help me how to interpret this ?
    >
    > void (*signal( int sig, void (*function)(int)) (int) );
    >


    Google for: "How to understand function prototype signal"
    Message-ID: <>

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jul 15, 2005
    #2
    1. Advertising

  3. Kenneth Guest

    That is a standard definition of funcition "signal".

    The statment declares a standard function(signal) returning a pointer
    to a function with an int parameter without return and the function
    signal also has two parameters, one of which is an integer, another is
    a function pointer to a function with a int parameter without return.

    Regards,
    Kenneth
    Kenneth, Jul 15, 2005
    #3
  4. Yohji Guest

    wrote:
    > I found the following declaration in some header file.
    > Can somebody help me how to interpret this ?
    >
    > void (*signal( int sig, void (*function)(int)) (int) );
    >
    >
    > Thanx for any help in advance ....

    Hmm,it is a little difficult.The signal functions is one of POSIX.
    The 'signal' returns a pointer which points to a function.This function
    is of 'void' type and has a para of int.Also,the 'signal' IS a
    function,it has two paras,one is int and the other is also a pointer
    pointing to a function.This function is not that function above,it has
    an int para and is of 'void' type.
    Oh,dizzy...
    Yohji, Jul 15, 2005
    #4
  5. Guest

    Kenneth wrote:
    > That is a standard definition of funcition "signal".
    >
    > The statment declares a standard function(signal) returning a pointer
    > to a function with an int parameter without return and the function
    > signal also has two parameters, one of which is an integer, another is
    > a function pointer to a function with a int parameter without return.
    >
    > Regards,
    > Kenneth


    Thanx for your help.
    I don't understand how function (signal) returns a pointer to function
    ?
    To declare a pointer to a function,
    we should have something like (*p)() as the precedence of function()
    is higher than "*" operator. But don't find the paranthesis to
    force proper association in the declaration of function signal.
    , Jul 15, 2005
    #5
  6. Yohji Guest

    wrote:
    > Kenneth wrote:
    > > That is a standard definition of funcition "signal".
    > >
    > > The statment declares a standard function(signal) returning a pointer
    > > to a function with an int parameter without return and the function
    > > signal also has two parameters, one of which is an integer, another is
    > > a function pointer to a function with a int parameter without return.
    > >
    > > Regards,
    > > Kenneth

    >
    > Thanx for your help.
    > I don't understand how function (signal) returns a pointer to function
    > ?
    > To declare a pointer to a function,
    > we should have something like (*p)() as the precedence of function()
    > is higher than "*" operator. But don't find the paranthesis to
    > force proper association in the declaration of function signal.


    In C, a function can of course return any kind of pointer,so it isn't
    strange that 'signal' returns a pointer to a function.Though we can use
    it like this:
    signal(...);
    but it is better to use it like this:
    (*signal)(...);
    just because it looks as it really is.
    Yohji, Jul 15, 2005
    #6
  7. Omri Barel Guest

    wrote:
    > I found the following declaration in some header file.
    > Can somebody help me how to interpret this ?
    >
    > void (*signal( int sig, void (*function)(int)) (int) );
    >
    >
    > Thanx for any help in advance ....
    >


    I can't compile this declaration. However, the standard (ANSI C)
    definition of signal (signal.h) is:

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

    Note the difference...

    Then you can look at it as follows:

    void (*something) (int)

    which means that something is a pointer to a function taking int
    returning void.

    something is:

    signal(int sig, parm)

    so signal is a function taking two parameters, an int and "parm", and
    returning a function taking an int returning void.

    parm is:

    void (*func)(int)

    which is a pointer to a function taking int returning void.

    So signal is:
    * a function,
    * taking two parameters,
    * returning a pointer to a function.

    The two parameters of signal are:
    * an int,
    * a pointer to a function which
    - takes an int,
    - returns void.

    The pointer signal returns is a pointer to a function which
    * takes an int,
    * returns void.


    Hope this helps.
    Omri Barel, Jul 15, 2005
    #7
  8. Guest

    Kenneth wrote:
    > That is a standard definition of funcition "signal".
    >
    > The statment declares a standard function(signal) returning a pointer
    > to a function with an int parameter without return and the function
    > signal also has two parameters, one of which is an integer, another is
    > a function pointer to a function with a int parameter without return.
    >
    > Regards,
    > Kenneth


    I feel instead of
    void (*signal( int sig, void (*function)(int)) (int) );

    it should be as follows:
    void (*signal( int sig, void (*function)(int))) (int);

    Still, I am not sure. Hope someone will point out the
    right one.
    , Jul 15, 2005
    #8
  9. Kenneth Guest

    I'm sorry I made a mistake.

    The correct standard declaration of function signal is as following
    line:
    void (*signal( int sig, void (*function)(int))) (int);

    Otherwise, anther form is wrong, which cannot be compiled.
    Kenneth, Jul 15, 2005
    #9
  10. wrote on 15/07/05 :
    > I found the following declaration in some header file.
    > Can somebody help me how to interpret this ?
    >
    > void (*signal( int sig, void (*function)(int)) (int) );
    >
    > Thanx for any help in advance ....


    Learn more about pointers to functions.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
    Emmanuel Delahaye, Jul 15, 2005
    #10
  11. Yohji wrote on 15/07/05 :
    > In C, a function can of course return any kind of pointer,so it isn't
    > strange that 'signal' returns a pointer to a function.Though we can use
    > it like this:
    > signal(...);


    At this is the Right Way.

    > but it is better to use it like this:
    > (*signal)(...);
    > just because it looks as it really is.


    Certainely not. You have no idea what function is the return value
    pointing to. It could return NULL...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
    Emmanuel Delahaye, Jul 15, 2005
    #11
  12. Kenneth wrote on 15/07/05 :
    > void (*signal( int sig, void (*function)(int))) (int);


    This is a case where typedefs are useful

    typedef int signal_f (int);

    signal_f signal (int sig, signal_f * function);

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
    Emmanuel Delahaye, Jul 15, 2005
    #12
  13. pete Guest

    Kenneth wrote:
    >
    > That is a standard definition of funcition "signal".
    >
    > The statment declares a standard function(signal) returning a pointer
    > to a function with an int parameter without return and the function
    > signal also has two parameters, one of which is an integer, another is
    > a function pointer to a function with a int parameter without return.


    This is a function prototype:
    void (*signal( int sig, void (*function)(int))) (int);

    It is not a definition and it is not a statement.

    --
    pete
    pete, Jul 15, 2005
    #13
  14. pete Guest

    pete wrote:
    >
    > Kenneth wrote:
    > >
    > > That is a standard definition of funcition "signal".
    > >
    > > The statment declares a standard function(signal)
    > > returning a pointer
    > > to a function with an int parameter without return and the function
    > > signal also has two parameters, one of which is an integer,
    > > another is a function pointer to a function
    > > with a int parameter without return.

    >
    > This is a function prototype:
    > void (*signal( int sig, void (*function)(int))) (int);
    >
    > It is not a definition and it is not a statement.


    Actually, it is a declaration of a pointer to a function type.

    --
    pete
    pete, Jul 15, 2005
    #14
  15. John Bode Guest

    wrote:
    > I found the following declaration in some header file.
    > Can somebody help me how to interpret this ?
    >
    > void (*signal( int sig, void (*function)(int)) (int) );
    >
    >
    > Thanx for any help in advance ....


    Here's how to break it down:

    signal -- signal
    signal() -- is a function
    signal( -- taking the parameters
    int sig, -- sig, which is an int,
    and
    function -- function
    *function -- which is a pointer
    (*function)() -- to a function
    (*function)(int) -- taking a single int
    parameter
    void (*function)(int) -- returning void
    )
    *signal(int sig, void (*function)(int)) -- returning a
    pointer
    (*signal(int sig, void (*function)(int)))() -- to a function
    (*signal(int sig, void (*function)(int)))(int) -- taking an int
    parameter
    void (*signal(int sig, void (*function)(int)))(int) -- returning void

    I think you miscopied the declaration above, which may contribute to
    your confusion.
    John Bode, Jul 15, 2005
    #15
  16. pete Guest

    Emmanuel Delahaye wrote:
    >
    > Kenneth wrote on 15/07/05 :
    > > void (*signal( int sig, void (*function)(int))) (int);

    >
    > This is a case where typedefs are useful
    >
    > typedef int signal_f (int);
    >
    > signal_f signal (int sig, signal_f * function);


    That gives my compiler an error:
    error C2091: function returns function

    This compiles OK:
    typedef int (*signal_f) (int);
    signal_f signal (int sig, signal_f * function);

    --
    pete
    pete, Jul 15, 2005
    #16
  17. pete Guest

    pete wrote:
    >
    > pete wrote:
    > >
    > > Kenneth wrote:
    > > >
    > > > That is a standard definition of funcition "signal".
    > > >
    > > > The statment declares a standard function(signal)
    > > > returning a pointer
    > > > to a function with an int parameter without return and the function
    > > > signal also has two parameters, one of which is an integer,
    > > > another is a function pointer to a function
    > > > with a int parameter without return.

    > >
    > > This is a function prototype:
    > > void (*signal( int sig, void (*function)(int))) (int);
    > >
    > > It is not a definition and it is not a statement.

    >
    > Actually, it is a declaration of a pointer to a function type.


    I might be a little confused there.
    I'm back to calling it a function prototype.

    --
    pete
    pete, Jul 15, 2005
    #17
  18. pete wrote:

    > pete wrote:
    >>
    >> This is a function prototype:
    >> void (*signal( int sig, void (*function)(int))) (int);
    >>
    >> It is not a definition and it is not a statement.

    >
    > Actually, it is a declaration of a pointer to a function type.


    Close, but no banana.

    Watch carefully, because the difference is a subtle one.

    It is clear that a function - or a pointer to function - is involved. We
    don't actually care, at this stage, what the parameter list is, so let's
    replace the whole list with X, giving:

    void (*signal(X))(int);

    We can now see that signal is a function, NOT a pointer to function. This
    function /returns/ a pointer to function. If this were a declaration of a
    pointer to a function type, it would be:

    void (*signal)(int);

    or perhaps:

    void (*signal)(X, int);

    but certainly not:

    void (*signal(X))(int);

    In case you don't see it, let's build up a declaration of our own. Let's
    write a function prototype for a function, foo (what else?), that both
    takes and returns a pointer to a function taking char and returning double;
    our function will also take a struct tm (as its first param), for good
    measure. We could cheat and do this:

    typedef double fp(char);
    fp *foo(struct tm, fp *);

    But let's avoid typedef for once. So how do we do it?

    We start off with foo, of course:

    foo

    foo is a function, so let's add the function syntax:

    foo()

    What does foo take? A pointer to a function taking char and returning
    double, and also a struct tm:

    foo(struct tm, double (*funcptr)(char))

    Now for the return value. We need to return a pointer to a function, not a
    pointer to double, so we can't say:

    double *foo.....(char);

    Instead, we need parens:

    double (*foo(struct tm, double (*funcptr)(char)))(char);

    and we're done. Compare signal:

    void (*signal( int sig, void (*function)(int))) (int);


    Conclusion: signal is indeed a function prototype, not a pointer to a
    function type, QED.

    But I don't blame you for getting that one wrong. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
    Richard Heathfield, Jul 15, 2005
    #18
  19. Guest

    John Bode wrote:
    > wrote:
    > > I found the following declaration in some header file.
    > > Can somebody help me how to interpret this ?
    > >
    > > void (*signal( int sig, void (*function)(int)) (int) );
    > >
    > >
    > > Thanx for any help in advance ....

    >
    > Here's how to break it down:
    >
    > signal -- signal
    > signal() -- is a function
    > signal( -- taking the parameters
    > int sig, -- sig, which is an int,
    > and
    > function -- function
    > *function -- which is a pointer
    > (*function)() -- to a function
    > (*function)(int) -- taking a single int
    > parameter
    > void (*function)(int) -- returning void
    > )
    > *signal(int sig, void (*function)(int)) -- returning a
    > pointer
    > (*signal(int sig, void (*function)(int)))() -- to a function
    > (*signal(int sig, void (*function)(int)))(int) -- taking an int
    > parameter
    > void (*signal(int sig, void (*function)(int)))(int) -- returning void
    >
    > I think you miscopied the declaration above, which may contribute to
    > your confusion.


    You are right. It should be
    void (*signal(int sig, void (*function)(int)))(int)
    instead of
    void (*signal(int sig, void (*function)(int))(int))

    That is why I was also confused. However, I did not try to compile
    it. Anyway, thanx for your help.
    , Jul 15, 2005
    #19
  20. pete Guest

    Richard Heathfield wrote:
    >
    > pete wrote:
    >
    > > pete wrote:
    > >>
    > >> This is a function prototype:
    > >> void (*signal( int sig, void (*function)(int))) (int);
    > >>
    > >> It is not a definition and it is not a statement.

    > >
    > > Actually, it is a declaration of a pointer to a function type.

    >
    > Close, but no banana.
    >
    > Watch carefully, because the difference is a subtle one.
    >
    > It is clear that a function
    > - or a pointer to function - is involved. We
    > don't actually care, at this stage, what the parameter list is,
    > so let's replace the whole list with X, giving:
    >
    > void (*signal(X))(int);
    >
    > We can now see that signal is a function,
    > NOT a pointer to function. This
    > function /returns/ a pointer to function.
    > If this were a declaration of a
    > pointer to a function type, it would be:
    >
    > void (*signal)(int);
    >
    > or perhaps:
    >
    > void (*signal)(X, int);
    >
    > but certainly not:
    >
    > void (*signal(X))(int);
    >
    > In case you don't see it,
    > let's build up a declaration of our own. Let's
    > write a function prototype for a function, foo (what else?), that both
    > takes and returns a pointer to a function taking
    > char and returning double;
    > our function will also take a struct tm (as its first param), for good
    > measure. We could cheat and do this:
    >
    > typedef double fp(char);
    > fp *foo(struct tm, fp *);
    >
    > But let's avoid typedef for once. So how do we do it?
    >
    > We start off with foo, of course:
    >
    > foo
    >
    > foo is a function, so let's add the function syntax:
    >
    > foo()
    >
    > What does foo take? A pointer to a function taking char and returning
    > double, and also a struct tm:
    >
    > foo(struct tm, double (*funcptr)(char))
    >
    > Now for the return value.
    > We need to return a pointer to a function, not a
    > pointer to double, so we can't say:
    >
    > double *foo.....(char);
    >
    > Instead, we need parens:
    >
    > double (*foo(struct tm, double (*funcptr)(char)))(char);
    >
    > and we're done. Compare signal:
    >
    > void (*signal( int sig, void (*function)(int))) (int);
    >
    > Conclusion: signal is indeed a function prototype, not a pointer to a
    > function type, QED.
    >
    > But I don't blame you for getting that one wrong. :)


    Thanks.
    I was confused.

    --
    pete
    pete, Jul 15, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aliki
    Replies:
    3
    Views:
    742
    LostAtC
    Sep 24, 2004
  2. James Hunt
    Replies:
    0
    Views:
    443
    James Hunt
    May 19, 2004
  3. Seetha J
    Replies:
    1
    Views:
    3,430
    Robbe Morris [C# MVP]
    May 17, 2005
  4. yzzzzz
    Replies:
    0
    Views:
    849
    yzzzzz
    Jan 7, 2005
  5. wenmang
    Replies:
    1
    Views:
    402
    Richard Tobin
    Jul 23, 2003
Loading...

Share This Page