简体   繁体   中英

Initializer lists in C and sequence points

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM