How to understand function prototype signal()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • None

    #1

    How to understand function prototype signal()

    Hello,

    1.
    The prototype of function signal in signal.h:

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


    is some complex to me. Would you please explain it to me in detail with
    the C language syntax itself. Thank you!



    2.
    And I saw another function prototype:

    void interrupt ( *oldhandler)();


    -Is it legal?

    -If it's a right prototype, what does identifier "interrupt" mean? Will
    it be a type qualifier.



    ---
    Thanks
    lovecreatesbeau ty

  • S.Tobias

    #2
    Re: How to understand function prototype signal()

    None <lovecreatesbea uty@gmail.com> wrote:

    [color=blue]
    > void (*signal(int sig, void (*func)(int)))( int);[/color]

    void (*)(int)

    type pointer to (function with one int parameter returning void)

    *signal()

    signal is a function returning pointer
    (Note:
    (*signal)()
    would mean: signal is a pointer to function)

    void (*signal())(int );

    signal is a function returning pointer to (function with one int
    param returning void)

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

    signal is a function with two parameters, returning pointer etc...
    The first parameter is type int, the second parameter is type:

    void (*)(int)

    ie. pointer to function with one int parameter returning void.


    The original declaration is equivalent to:
    void (*signal(int, void (*)(int)))(int) ;
    ie. the parameters of `signal' don't have to be named, similarly
    parameter to `func' was not named.

    Note that the type of the second parameter of `signal' is the same
    as the type that `signal' returns, ie. "pointer to (fn with one int
    param, returning void)".

    It's easier to understand if you declare a type:

    typedef void (*ptr_void_fn_i nt)(int);
    ptr_void_fn_int signal(int, ptr_void_fn_int );

    (ptr_void_fn_in t is type "pointer to (fn with one etc...)").

    `signal' function for a given signal number sets the new signal
    handler you supply (which is type ptr_void_fn_int ) and returns
    you the previous handler (which naturally has to be the same type).
    (For exact `signal' semantics see N869 7.14.1.1.)

    [color=blue]
    > void interrupt ( *oldhandler)();[/color]
    [color=blue]
    > -Is it legal?[/color]
    [color=blue]
    > -If it's a right prototype, what does identifier "interrupt" mean? Will
    > it be a type qualifier.[/color]

    `interrupt' is not an ISO C keyword. It probably denotes function
    call convention, and means that the function `oldhandler' points to
    will be called from outside as an interrupt handler (but I might be
    very wrong here). You have to consult your compiler manual.

    --
    Stan Tobias
    mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

    Comment

    • lovecreatesbeauty

      #3
      Re: How to understand function prototype signal()

      Stan Tobias,

      Thank you

      S.Tobias wrote:[color=blue]
      > None <lovecreatesbea uty@gmail.com> wrote:
      >
      >[color=green]
      > > void (*signal(int sig, void (*func)(int)))( int);[/color]
      >
      > void (*)(int)
      >
      > type pointer to (function with one int parameter returning void)
      >
      > *signal()
      >
      > signal is a function returning pointer
      > (Note:
      > (*signal)()
      > would mean: signal is a pointer to function)
      >
      > void (*signal())(int );
      >
      > signal is a function returning pointer to (function with one int
      > param returning void)
      >
      > void (*signal(int sig, void (*func)(int)))( int);
      >
      > signal is a function with two parameters, returning pointer etc...
      > The first parameter is type int, the second parameter is type:
      >
      > void (*)(int)
      >
      > ie. pointer to function with one int parameter returning void.
      >
      >
      > The original declaration is equivalent to:
      > void (*signal(int, void (*)(int)))(int) ;
      > ie. the parameters of `signal' don't have to be named, similarly
      > parameter to `func' was not named.
      >
      > Note that the type of the second parameter of `signal' is the same
      > as the type that `signal' returns, ie. "pointer to (fn with one int
      > param, returning void)".
      >
      > It's easier to understand if you declare a type:
      >
      > typedef void (*ptr_void_fn_i nt)(int);
      > ptr_void_fn_int signal(int, ptr_void_fn_int );
      >
      > (ptr_void_fn_in t is type "pointer to (fn with one etc...)").
      >
      > `signal' function for a given signal number sets the new signal
      > handler you supply (which is type ptr_void_fn_int ) and returns
      > you the previous handler (which naturally has to be the same type).
      > (For exact `signal' semantics see N869 7.14.1.1.)
      >
      >[color=green]
      > > void interrupt ( *oldhandler)();[/color]
      >[color=green]
      > > -Is it legal?[/color]
      >[color=green]
      > > -If it's a right prototype, what does identifier "interrupt" mean?[/color][/color]
      Will[color=blue][color=green]
      > > it be a type qualifier.[/color]
      >
      > `interrupt' is not an ISO C keyword. It probably denotes function
      > call convention, and means that the function `oldhandler' points to
      > will be called from outside as an interrupt handler (but I might be
      > very wrong here). You have to consult your compiler manual.
      >
      > --
      > Stan Tobias
      > mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`[/color]

      Comment

      Working...