等效的 C 声明

发布于 2024-10-26 14:03:24 字数 975 浏览 5 评论 0原文

是等价的吗

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
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! 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梓梦 2024-11-02 14:03:24

阅读声明时请遵循这个简单的过程

从变量名称开始(或
如果没有标识符则最内层构造
存在。向右看而不跳
在右括号上;说你什么
看。再向左看一次,不要跳
在括号上;说出你所看到的。
跳出一级括号 if
任何。向右看;说出你所看到的。
向左看;说出你所看到的。继续
以这种方式,直到你说出
变量类型或返回类型。

因此:

int (*x)[10];

x 是一个指向 10 个 int 数组的指针

int x[10];

x 是一个由 10 个 int 组成的数组

int *x[10];

< code>x 是一个由 10 个指向 int 的指针组成的数组

Follow this simple process when reading declarations:

Start at the variable name (or
innermost construct if no identifier
is present. Look right without jumping
over a right parenthesis; say what you
see. Look left again without jumping
over a parenthesis; say what you see.
Jump out a level of parentheses if
any. Look right; say what you see.
Look left; say what you see. Continue
in this manner until you say the
variable type or return type.

So:

int (*x)[10];

x is a pointer to an array of 10 ints

int x[10];

x is an array of 10 ints

int *x[10];

x is an array of 10 pointers to ints

疾风者 2024-11-02 14:03:24

他们不平等。在第一种情况下,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 case x 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.

岁月蹉跎了容颜 2024-11-02 14:03:24

我倾向于遵循理解 C 声明的优先规则,这在书中很好地给出了 专家 C 编程 - 深层 C 秘密 作者:Peter van der Linden

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.

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 - 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.
沫尐诺 2024-11-02 14:03:24

对我来说,更容易记住规则,因为没有任何显式分组,()[]* 之前绑定。因此,对于像 [] 这样的声明,

T *a[N];

绑定在 * 之前,因此 a 是一个 N 元素指针数组。分步骤分解:

   a     -- a
   a[N]  -- is an N-element array
  *a[N]  -- of pointer
T *a[N]  -- to T.

对于像括号这样的声明,

T (*a)[N];

强制 *[] 之前绑定,所以

    a      -- a
  (*a)     -- is a pointer
  (*a)[N]  -- to an N-element array
T (*a)[N]  -- of T

它仍然是顺时针/螺旋规则,只是以更详细的形式表达紧凑的方式。

For me, it's easier to remember the rule as absent any explicit grouping, () and [] bind before *. Thus, for a declaration like

T *a[N];

the [] bind before the *, so a is an N-element array of pointer. Breaking it down in steps:

   a     -- a
   a[N]  -- is an N-element array
  *a[N]  -- of pointer
T *a[N]  -- to T.

For a declaration like

T (*a)[N];

the parens force the * to bind before the [], so

    a      -- a
  (*a)     -- is a pointer
  (*a)[N]  -- to an N-element array
T (*a)[N]  -- of T

It's still the clockwise/spiral rule, just expressed in a more compact manner.

淡莣 2024-11-02 14:03:24

不。第一个声明一个包含 10 个 int 指针的数组,第二个声明一个包含 10 个 int 的数组。

No. First one declares an array of 10 int pointers and second one declares an array of 10 ints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文