相当于C声明 [英] Equivalent C declarations
问题描述
是
INT(* X)[10];
和
INT X [10];
等同?
按照顺时针螺旋统治,他们要分析不同的C声明。
有关的点击疲惫:
的``顺时针/螺旋规则''大卫·
安德森
有是被称为一技术
``顺时针/螺旋规则',这
使任何C程序员在解析
他们的头任何C宣言!
有三个简单的步骤如下:
块引用>1.未知的元素开始,移动螺旋/顺时针方向;
ecountering以下元素在与替换它们
相应的英文语句: [X]或[]
= GT;阵列X规格...或数组未定义大小...
(TYPE1,TYPE2)
= GT;函数传递Type1和Type2返回...
*
= GT;指针(县)... 2.保持在一个螺旋/顺时针方向做下去,直到所有令牌已经覆盖。 3.总是先解决在括号中的东西!
解决方案他们是不相等的。在第一种情况下
X
是一个指向10整数数组,在第二种情况下X
是数组10的整数。的两种类型是不同的。你可以看到他们是不一样的东西通过检查
的sizeof
在这两种情况。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 Anderson
There 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!
解决方案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声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!