问题描述
此问题如下<一个href=\"http://stackoverflow.com/questions/3706704/whats-the-meaning-of-this-piece-of-$c$c-void-signalint-sig-void-funcint\">this关于C声明的其他问题。读这个问题的答案,我看了一下,我也明白什么是声明如下用途的意思。
This question follows this other question about C declarations. Reading the answer to this question, I read about the spiral rule and I also understood what "declaration follows usage" means.
好了为止。但后来我读:
Ok so far. But then I read this declaration:
char *(*(*a[N])())();
和我想知道如何与解析声明如下用法规则。特别是对阵列的部分。
and I was wondering how to parse it with the "declaration follows usage" 'rule'. Especially for the array part.
我读的是:
(*(*a[N])())
是一个函数()
返回的char *
,那么,取消引用以下
is a function ()
returning a char *
, then, dereferencing the following
(*a[N])() // 1
这是'函数返回一个的char *
',所以1是一个指针返回一个函数的char *
那么我会说'当(* A [N])
被调用,它是[previous声明]。在这一点上,我们有(* A [N])
是函数返回一个指针,返回一个函数的char *
is this 'function returning a char*
', and so 1 is a 'pointer to a function returning char *
' then I would say 'when (*a[N])
is called, it is [previous declaration]'. At this point we have (*a[N])
is a function returning a pointer to a function returning char *
.
但后来我不知道如何应用此规则的指针数组和指针数组区别开来。
But then I don't know how to apply this rule to distinguish between the 'pointer to array' and 'array of pointer'.
有人能澄清这?
其他的问题:什么是这样的声明precedence之间的规则&安培;
(在C ++), *
和 []
? [也许precedence不正确的说法]
Other question: what are the rules of "precedence" in such declarations, between &
(in C++), *
, and []
? [maybe "precedence" is not the correct term]
要检查,如果我明白了螺旋规则正确,我也解析此前$ P $下面pssion;请告诉我,如果我错了。
To check if I understood the 'spiral rule' correctly, I also parse this expression below; please tell me if I am wrong.
+-----------+
| +------+ |
| | +-+ | |
| | ^ | | |
char* (*(* a[N])())();
^ ^ ^ ^ ^ ^
| | | | | |
| | +---+ | |
| +--------+ |
+--------------+
对于我来说更容易(通过循环回路):
For me it is easier (loop by loop):
-
A
是N阵列... - 指针函数返回...
- 指针函数返回...
-
的char *
a
is an array of N ...- pointer to function returning ...
- pointer to function returning ...
char *
但我或许缺少一些东西,在这种情况下,让我得到正确的答案,但可能是错误的另一种更复杂的情况。
But I am maybe missing something which in that case let me obtain the correct answer but that could be wrong in another more complicated case.
推荐答案
您只需要建立起来的步骤。
You just have to build it up in steps.
char *X(); // X =~ (*(*a[N])())
函数返回的char *
char *(*Y())(); // Y =~ (*a[N])
函数返回指针函数返回的char *
。
在一个声明中,就像在一个前pression [] 具有更高的precedence是一元 *
所以 *一个[N]
等同于 *(A [N])
,而不是(*一)[N]
。
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]
.
char *(*(*Z)())(); // Z =~ a[N]
函数指针返回指针函数返回的char *
。
char *(*(*a[N])())();
n个指针数组给函数返回一个指针函数返回的char *
。
这篇关于螺旋规则和“声明如下使用”解析C和C ++声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!