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

Initializer lists in C and sequence points

C 标准声明在完整的末尾有一个序列点 初始化器中的表达式和那个

initializer:

        assignment-expression

        { initializer-list }

        { initializer-list , }

initializer-list:

        initializer

        initializer-list , initializer

然而,这意味着这

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

应该没问题。有人能解释一下为什么会这样吗?

我不知道你在哪里看到的。我看到 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

它“很好”,因为行为 定义unspecified behavior。你不知道,i = 1++i 中的哪一个会先执行或最后执行,其中一个会。