等效的 C 声明
是等价的吗
int (*x)[10];
和
int x[10];
?
根据“顺时针螺旋”规则,它们解析为不同的C声明。
对于厌倦点击的人:
大卫的“顺时针/螺旋法则” 安德森
有一种技术称为 “顺时针/螺旋规则” 使任何 C 程序员都能够解析 他们的头部有任何 C 声明!
需要遵循三个简单的步骤:
1. Starting with the unknown element, move in a spiral/clockwise direction;
when ecountering the following elements replace them with the
corresponding english statements:
[X] or []
=> Array X size of... or Array undefined size of...
(type1, type2)
=> function passing type1 and type2 returning...
*
=> pointer(s) to...
2. Keep doing this in a spiral/clockwise direction until all tokens have been covered.
3. Always resolve anything in parenthesis first!
Are
int (*x)[10];
and
int x[10];
equivalent?
According to the "Clockwise Spiral" rule, they parse to different C declarations.
For the click-weary:
The ``Clockwise/Spiral Rule'' By David
AndersonThere is a technique known as the
``Clockwise/Spiral Rule'' which
enables any C programmer to parse in
their head any C declaration!There are three simple steps to follow:
1. Starting with the unknown element, move in a spiral/clockwise direction;
when ecountering the following elements replace them with the
corresponding english statements:
[X] or []
=> Array X size of... or Array undefined size of...
(type1, type2)
=> function passing type1 and type2 returning...
*
=> pointer(s) to...
2. Keep doing this in a spiral/clockwise direction until all tokens have been covered.
3. Always resolve anything in parenthesis first!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
阅读声明时请遵循这个简单的过程:
因此:
x
是一个指向 10 个int
数组的指针x
是一个由 10 个int
组成的数组< code>x 是一个由 10 个指向
int
的指针组成的数组Follow this simple process when reading declarations:
So:
x
is a pointer to an array of 10int
sx
is an array of 10int
sx
is an array of 10 pointers toint
s他们不平等。在第一种情况下,
x
是指向包含 10 个整数的数组的指针,在第二种情况下,x
是包含 10 个整数的数组。这两种类型是不同的。通过检查这两种情况下的
sizeof
,您可以发现它们不是同一件事。They are not equal. in the first case
x
is a pointer to an array of 10 integers, in the second casex
is an array of 10 integers.The two types are different. You can see they're not the same thing by checking
sizeof
in the two cases.我倾向于遵循理解 C 声明的优先规则,这在书中很好地给出了 专家 C 编程 - 深层 C 秘密 作者:Peter van der Linden
I tend to follow The Precedence Rule for Understanding C Declarations which is given very nicely in the book Expert C Programming - Deep C Secrets by Peter van der Linden
对我来说,更容易记住规则,因为没有任何显式分组,
()
和[]
在*
之前绑定。因此,对于像[]
这样的声明,绑定在
*
之前,因此a
是一个 N 元素指针数组。分步骤分解:对于像括号这样的声明,
强制
*
在[]
之前绑定,所以它仍然是顺时针/螺旋规则,只是以更详细的形式表达紧凑的方式。
For me, it's easier to remember the rule as absent any explicit grouping,
()
and[]
bind before*
. Thus, for a declaration likethe
[]
bind before the*
, soa
is an N-element array of pointer. Breaking it down in steps:For a declaration like
the parens force the
*
to bind before the[]
, soIt's still the clockwise/spiral rule, just expressed in a more compact manner.
不。第一个声明一个包含 10 个 int 指针的数组,第二个声明一个包含 10 个 int 的数组。
No. First one declares an array of 10 int pointers and second one declares an array of 10 ints.