How to understand function prototype signal()

N

None

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.
 
S

S.Tobias

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

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_int)(int);
ptr_void_fn_int signal(int, ptr_void_fn_int);

(ptr_void_fn_int 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.)

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

`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.
 
L

lovecreatesbeauty

Stan Tobias,

Thank you

S.Tobias said:
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_int)(int);
ptr_void_fn_int signal(int, ptr_void_fn_int);

(ptr_void_fn_int 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.)





`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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top