什么是这片code的含义?无效(*信号(INT SIG,无效(* FUNC)(INT)))(INT); [英] What's the meaning of this piece of code? void (*signal(int sig, void (*func)(int)))(int);
问题描述
我碰到这片code,彻底迷路了间preting它的意义。
I came across this piece of code and completely got lost interpreting its meaning.
#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);
什么是在2号线为code详细解释?
What is a detailed explanation for the code at line 2?
我知道无效
和 INT
的类型,在* func为一个函数指针,而括号内是当务之急。但我仍然没有得到(*信号...),在(INT),以及整个事情结合在一起。越详细越好。
I know that void
and int
are types, the *func is a pointer for a function, and the brackets are for priority. But I still don't get the (*signal ...), the (int), and the whole thing combined together. The more detailed, the better.
也许我知道这个声明的意义/效果。但是,我不得不做出一些更多的试验来帮助我明白是怎么回事,如下:
Probably I've known the meaning/effect of this declaration. But I had to make some more trials to help me understand what's going on, as below:
1 #include <signal.h>
2 void (*signal)(int sig, void (*func)(int));
3 void (*signal)(int); // then void (signal)(int) again.
4 //void (*signal(int sig, void (*func)(int)))(int); //break this line into two lines above
5
6 int main(){}
在上面的code,我打破了无效(*信号(INT SIG,无效(* FUNC)(INT)))(INT)
成两行。 3行,我都试过无效(*信号)(INT)
和无效(信号)(INT)
,与表示,我试图重新声明信号
同样的错误结果:
In the above code, I broke void (*signal(int sig, void (*func)(int)))(int)
into two lines. For line 3, I tried both void (*signal)(int)
and void (signal)(int)
, with the same error result that indicated that I was trying to redeclare signal
:
TestDeclaration.c:2:错误:'信号'重新声明为不同意义的符号
/usr/include/signal.h:93:错误:的'信号'previous声明在这里结果
TestDeclaration.c:3:错误:'信号'重新声明为不同意义的符号
/usr/include/signal.h:93:错误:的'信号'previous声明在这里
TestDeclaration.c:2: error: 'signal' redeclared as different kind of symbol /usr/include/signal.h:93: error: previous declaration of 'signal' was here
TestDeclaration.c:3: error: 'signal' redeclared as different kind of symbol /usr/include/signal.h:93: error: previous declaration of 'signal' was here
现在我知道这两个试验是申报不正确的方式,但为什么他们不正确的?为什么声明的原始的方式不是一个重声明?
Now I know both the trials are incorrect ways of declaration, but why are they incorrect? Why is the original way of declaration NOT a redeclaration?
推荐答案
这是一个功能服用声明的 INT
和一个指向一个函数(取 INT
返回void),并返回一个指针的函数(取 INT
键,返回void)。
It's the declaration of a function taking an int
and a pointer to a function (taking int
returning void) and returning a pointer to a function (taking int
and returning void).
的说明,或引导间pretation
您可以通过将一个单一的实体在括号治疗的一切,然后使用向内工作间preT声明如下用途规则。
You can interpret by treating everything in parentheses as a single entity and then working inwards using the "declaration follows usage" rule.
无效<击>(*信号(INT SIG,无效(* FUNC)(INT)))击>(INT);
在括号中的实体看起来像一个函数以 INT
并返回无效
。
The entity in the brackets looks like a function taking int
and returning void
.
剥去外面部分:
*signal(int sig, void (*func)(int))
因此,信号
需要一些参数,返回的东西,可以解除引用(由于领先 *
)以形成功能以 INT
并返回无效
。
So, signal
takes some parameters and returns something that can be dereferenced (due to the leading *
) to form a function taking int
and returning void
.
这意味着信号
返回一个指针函数的函数(取 INT
并返回无效
)。
This means signal
is a function returning a pointer to a function (taking int
and returning void
).
看着它需要一个 INT
(即 SIG
)和无效的参数( * FUNC)(INT)
这是一个指向一个函数(取 INT
并返回无效
)。
Looking at the parameters it takes an int
(i.e. sig
) and void (*func)(int)
which is a pointer to a function (taking int
and returning void
).
这篇关于什么是这片code的含义?无效(*信号(INT SIG,无效(* FUNC)(INT)))(INT);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!