C 中的初始化列表和序列点

发布于 2025-01-17 07:36:57 字数 342 浏览 1 评论 0原文

C 标准规定,在完整的末尾有一个序列点 然而,这

initializer:

        assignment-expression

        { initializer-list }

        { initializer-list , }

initializer-list:

        initializer

        initializer-list , initializer

意味着这

int a[2] = { i = 1 , ++i };

应该没问题。有人可以解释一下为什么会出现这种情况吗?

The C Standard states that there is a sequence point at the end of a full
expression in an initializer and that

initializer:

        assignment-expression

        { initializer-list }

        { initializer-list , }

initializer-list:

        initializer

        initializer-list , initializer

That would mean, however, that this

int a[2] = { i = 1 , ++i };

ought to be fine. Could someone please explain why, or why not, this is the case?

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

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

发布评论

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

评论(1

鸵鸟症 2025-01-24 07:36:57

我不知道你在哪里看到的。我看到 https://port70.net/~nsz/c/ c11/n1570.html#6.7.9p23

初始化列表表达式的计算彼此之间的顺序不确定,因此任何副作用发生的顺序是未指定的。

应该没问题。有人可以解释一下原因吗

它“很好”,因为行为被定义为未指定行为。您不知道 i = 1++i 中的哪一个将首先执行或最后执行,其中一个会执行。

I do not know where you see that. I see https://port70.net/~nsz/c/c11/n1570.html#6.7.9p23 :

The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified.

ought to be fine. Could someone please explain why

It is "fine", as in the behavior is defined to be unspecified behavior. You do not know, which one of i = 1 or ++i will execute first or last, one of them will.

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