c++ - Spiral rule and 'declaration follows usage' for parsing C expressions - Stack Overflow
You just have to build it up in steps.c - what's the meaning of this piece of code? void (signal(int sig, void (func)(int)))(int); - Stack Overflow
Function returningchar *X(); // X =~ (*(*a[N])())
char*
Function returning pointer to function returningchar *(*Y())(); // Y =~ (*a[N])
char*
.
In a declaration, just as in an expression (declaration follow usage), postfix[]
has a higher precedence that unary*
so*a[N]
is equivalent to*(a[N])
, not(*a)[N]
.
Pointer to function returning pointer to function returningchar *(*(*Z)())(); // Z =~ a[N]
char*
.
Array of N pointers to functions returning a pointer to function returningchar *(*(*a[N])())();
char*
.
#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);
It's the declaration of a function taking anint
and a pointer to a function (takingint
returning void) and returning a pointer to a function (takingint
and returning void).
Explanation, or guide to interpretation
You can interpret by treating everything in parentheses as a single entity and then working inwards using the "declaration follows usage" rule.
void (*signal(int sig, void (*func)(int)))(int);
The entity in the brackets looks like a function takingint
and returningvoid
.
Stripping away the outer part:
*signal(int sig, void (*func)(int))
So,signal
takes some parameters and returns something that can be dereferenced (due to the leading*
) to form a function takingint
and returningvoid
.
This meansThe guy deserves the 128 thousand points of reputation.signal
is a function returning a pointer to a function (takingint
and returningvoid
).
Looking at the parameters it takes anint
(i.e.sig
) andvoid (*func)(int)
which is a pointer to a function (takingint
and returningvoid
).
No comments:
Post a Comment
Before writing, please read carefully my policy for comments. Some comments may be deleted.
Please do not include links to commercial or unrelated sites in your comment or signature, or I'll flag it as SPAM.