等价C声明
是 及 等价物 根据规则,它们解析为不同的C声明 对于“单击疲劳”: 大卫的“顺时针/螺旋法则” 安德森 有一种技术被称为 ``顺时针/螺旋规则“”,其中 允许任何C程序员在 他们的头有任何C声明 以下是三个简单的步骤:等价C声明,c,declaration,C,Declaration,是 及 等价物 根据规则,它们解析为不同的C声明 对于“单击疲劳”: 大卫的“顺时针/螺旋法则” 安德森 有一种技术被称为 ``顺时针/螺旋规则“”,其中 允许任何C程序员在 他们的头有任何C声明 以下是三个简单的步骤: 否。第一个声明一个包含10个int指针的数组,第二个声明一个包含10个int的数组。读取声明时遵循以下步骤: 从变量名(或)开始 如果没有标识符,最里面的构造 有人在场。看右边不要跳 在右括号上方;说出你想说的话 看,再向左看,不要跳 在括号上方;说出你所看到的。 如果需要,请
否。第一个声明一个包含10个int指针的数组,第二个声明一个包含10个int的数组。读取声明时遵循以下步骤: 从变量名(或)开始 如果没有标识符,最里面的构造 有人在场。看右边不要跳 在右括号上方;说出你想说的话 看,再向左看,不要跳 在括号上方;说出你所看到的。 如果需要,请跳出一级括号 随便。看右边,说你看到的。 向左看;说出你看到的。继续 以这种方式,直到你说 变量类型或返回类型 因此:
x
是指向10int
s数组的指针
int (*x)[10];
int x[10];
x
是由10int
s组成的数组
int (*x)[10];
int x[10];
x
是指向int
s的10个指针的数组,它们不相等。在第一种情况下,x
是指向10个整数数组的指针,在第二种情况下,x
是10个整数的数组
int (*x)[10];
int x[10];
这两种类型是不同的。通过检查这两种情况下的
sizeof
,可以看出它们是不同的。我倾向于遵循理解C声明的优先规则,这在Peter van der Linden的书中给出得很好
int *x[10];
对我来说,更容易记住没有任何显式分组的规则,
()
和[]
在*
之前绑定
A - Declarations are read by starting with the name and then reading in
precedence order.
B - The precedence, from high to low, is:
B.1 parentheses grouping together parts of a declaration
B.2 the postfix operators:
parentheses () indicating a function, and
square brackets [] indicating an array.
B.3 the prefix operator: the asterisk denoting "pointer to".
C If a const and/or volatile keyword is next to a type specifier (e.g. int,
long, etc.) it applies to the type specifier.
Otherwise the const and/or volatile keyword
applies to the pointer asterisk on its immediate left.
a -- a
a[N] -- is an N-element array
*a[N] -- of pointer
T *a[N] -- to T.
[]
绑定在*
之前,因此a
是指针的N元素数组。将其分解为以下步骤:
T *a[N];
对于像这样的声明
A - Declarations are read by starting with the name and then reading in
precedence order.
B - The precedence, from high to low, is:
B.1 parentheses grouping together parts of a declaration
B.2 the postfix operators:
parentheses () indicating a function, and
square brackets [] indicating an array.
B.3 the prefix operator: the asterisk denoting "pointer to".
C If a const and/or volatile keyword is next to a type specifier (e.g. int,
long, etc.) it applies to the type specifier.
Otherwise the const and/or volatile keyword
applies to the pointer asterisk on its immediate left.
a -- a
a[N] -- is an N-element array
*a[N] -- of pointer
T *a[N] -- to T.
paren强制*
在[]
之前绑定,因此
T (*a)[N];
这仍然是顺时针/螺旋规则,只是以更简洁的方式表示。不,第一个是指向10个整数数组的指针(你几乎不需要这样的指针)。10个整数指针数组应该是
int*x[10]
我倾向于相信你是错误的,10个指针数组的语法是int*x[10];
这与“顺时针螺旋线”完全相同规则。我的问题没有答案。@crypto-我很困惑。你为什么认为你的问题没有答案?@crypto-你为什么要用我下面提供的答案更新你的问题?我想高票是因为这个问题引用了顺时针螺旋规则。我对C非常精通,以前从未听说过e、 @lwburk,我在我的问题中逐字引用了超链接中的广告,这在编辑之前就已经存在了。@crypto-那么我对你的困惑感到困惑。如果你已经知道它们是不同的,那么你为什么要问它们是否不同?你是在问为什么“x是指向10个整数数组的指针”与“x是指向整数的10个指针的数组“?@crypto:如果你知道它们是不等价的,并且这里的每个人都验证了它们是不等价的,那么我很困惑你为什么声称这个问题没有答案?你没有说明什么明显的事情?除非我们都遗漏了什么,否则我倾向于给出一个-1和六个变量。