How to interpret this declaration

J

junky_fellow

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

S.Tobias

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: <[email protected]>
 
K

Kenneth

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
 
Y

Yohji

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

junky_fellow

Kenneth said:
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.
 
Y

Yohji

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

Omri Barel

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

junky_fellow

Kenneth said:
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.
 
K

Kenneth

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

Emmanuel Delahaye

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?"
 
E

Emmanuel Delahaye

P

pete

Kenneth said:
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.
 
P

pete

pete said:
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.
 
J

John Bode

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

pete

Emmanuel said:
Kenneth wrote on 15/07/05 :

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);
 
R

Richard Heathfield

pete said:
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. :)
 
J

junky_fellow

John said:
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.
 
P

pete

Richard said:
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.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top