Home > Archive > Unix Programming > March 2008 > Unable to understand signal function decleration
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Unable to understand signal function decleration
|
|
| Sanchit 2008-03-20, 7:15 pm |
| #include <signal.h>
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
| |
| Martin York 2008-03-20, 7:15 pm |
| On Mar 20, 7:10 am, Sanchit <sanchitgupt...@gmail.com> wrote:
> #include <signal.h>
>
> 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.
| |
| Ben Bacarisse 2008-03-20, 7:15 pm |
| Sanchit <sanchitgupta.1@gmail.com> writes:
> #include <signal.h>
>
> 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:
<R> (*f( <parameters-of-f> )) ( <P> )
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( <some parameters> )) (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.
| |
| Sanchit 2008-03-20, 7:15 pm |
| Thanks a lot Ben and Martin!!!!!!!
Your explanations cleared my doubt!
| |
| Sanchit 2008-03-20, 7:15 pm |
| On Mar 20, 7:57 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Sanchit <sanchitgupt...@gmail.com> writes:
>
>
>
> 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:
>
> <R> (*f( <parameters-of-f> )) ( <P> )
>
> 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( <some parameters> )) (int);
>
> i.e. that signal returns a pointer to a function that takes a single
> int and which return void.
>
>
>
> 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 <Some parameter> here?
| |
| David Schwartz 2008-03-20, 7:15 pm |
| On Mar 20, 8:44 am, Sanchit <sanchitgupt...@gmail.com> wrote:
[color=darkred]
> You said that P is the parameter then what are <Some parameter> 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
| |
| Sanchit 2008-03-21, 4:36 am |
| On Mar 20, 10:11 pm, David Schwartz <dav...@webmaster.com> wrote:
> On Mar 20, 8:44 am, Sanchit <sanchitgupt...@gmail.com> wrote:
>
>
> 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
| |
| jason.cipriani@gmail.com 2008-03-21, 4:36 am |
| On Mar 20, 11:44 am, Sanchit <sanchitgupt...@gmail.com> wrote:
> On Mar 20, 7:57 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> You said that P is the parameter then what are <Some parameter> here?
Read this:
http://www.ericgiguere.com/articles...ml?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.
|
|
|
|
|