C 定义中的运算符优先级

发布于 2024-10-13 18:10:08 字数 284 浏览 5 评论 0原文

Wikipedia 声称 [] 运算符优先于求值中的 * 运算符。

那么,为什么下面的语句:

char *a[3];

声明一个 3 个字符指针的数组,而不是根据运算符优先级声明一个指向 3 个字符数组的指针?

Wikipedia claims that the [] operator precedes the * operator in evaluation.

Then, why does the following statement:

char *a[3];

declare an array of 3 character pointers, rather than a pointer to an array of 3 characters as per the operator precedence?

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

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

发布评论

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

评论(3

居里长安 2024-10-20 18:10:08

因为,正如维基百科所说,[] 的优先级高于 *

处理声明时,a[3] 将在处理 * 之前作为“3 的数组”进行处理。

要声明指向三个字符数组的指针,必须使用括号来覆盖默认优先级:

char (*a)[3];

现在括号优先于数组。

Because, as Wikipedia says, [] has higher precedence than *?

Processing the declaration, the a[3] is processed as 'array of 3' before you process the *.

To declare a pointer to an array of three characters, you have to use parentheses to override the default precedence:

char (*a)[3];

Now the parentheses take precedence over the array.

灰色世界里的红玫瑰 2024-10-20 18:10:08

以下是声明符的语法,取自标准 (§ 6.7.5):

declarator:
    pointeropt direct-declarator

direct-declarator:
    identifier
    ( declarator )
    direct-declarator [ type-qualifier-listopt assignment-expressionopt ]
    direct-declarator [ static type-qualifier-listopt assignment-expression ]
    direct-declarator [ type-qualifier-list static assignment-expression ]
    direct-declarator [ type-qualifier-listopt * ]
    direct-declarator ( parameter-type-list )
    direct-declarator ( identifier-listopt )

pointer:
    * type-qualifier-listopt
    * type-qualifier-listopt pointer

type-qualifier-list:
    type-qualifier
    type-qualifier-list type-qualifier

parameter-type-list:
    parameter-list
    parameter-list , ...

parameter-list:
    parameter-declaration
    parameter-list , parameter-declaration

parameter-declaration:
    declaration-specifiers declarator
    declaration-specifiers abstract-declaratoropt

identifier-list:
    identifier
    identifier-list , identifier

如您所见,[]() 都绑定到 * 之前的声明符。取声明声明

int *a[N];

符为*a[N],它符合上面的pointeropt直接声明符模式,因此被解析为< code>*(a[N]),所以a是一个N元素的指针数组。

总结:

T *a[N]      -- declares an N-element array of pointer to T
T (*a)[N]    -- declares a pointer to an N-element array of T
T *f()       -- declares a function returning pointer to T
T (*f)()     -- declares a pointer to a function returning T  

Here's the grammar for a declarator as taken from the standard (§ 6.7.5):

declarator:
    pointeropt direct-declarator

direct-declarator:
    identifier
    ( declarator )
    direct-declarator [ type-qualifier-listopt assignment-expressionopt ]
    direct-declarator [ static type-qualifier-listopt assignment-expression ]
    direct-declarator [ type-qualifier-list static assignment-expression ]
    direct-declarator [ type-qualifier-listopt * ]
    direct-declarator ( parameter-type-list )
    direct-declarator ( identifier-listopt )

pointer:
    * type-qualifier-listopt
    * type-qualifier-listopt pointer

type-qualifier-list:
    type-qualifier
    type-qualifier-list type-qualifier

parameter-type-list:
    parameter-list
    parameter-list , ...

parameter-list:
    parameter-declaration
    parameter-list , parameter-declaration

parameter-declaration:
    declaration-specifiers declarator
    declaration-specifiers abstract-declaratoropt

identifier-list:
    identifier
    identifier-list , identifier

As you can see, both [] and () bind to the declarator before *. Take the declaration

int *a[N];

The declarator is *a[N], which fits the pointeropt direct-declarator pattern above, and is thus parsed as *(a[N]), so a is an N-element array of pointer.

To sum up:

T *a[N]      -- declares an N-element array of pointer to T
T (*a)[N]    -- declares a pointer to an N-element array of T
T *f()       -- declares a function returning pointer to T
T (*f)()     -- declares a pointer to a function returning T  
涙—继续流 2024-10-20 18:10:08

我对这个问题感到困惑 - 声明的解释与运算符优先级相匹配。如果您想要一个指向数组的指针,则必须在 [] 绑定之前使用括号“将 * 绑定到标识符”。

char (*a)[3];

I'm confused about the question - the interpretation of the declaration matches the operator precedence. If you want a pointer to an an array you have to use parens to 'bind the * to the indentifier' before the [] binding.

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