C语言 C中的布尔数组初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25470241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:17:31  来源:igfitidea点击:

Boolean array initialization in C

carrays

提问by user3533671

I've stumbled upon some weird behavior for which I couldn't find any info online. If I initialize a boolean array like this:

我偶然发现了一些奇怪的行为,我在网上找不到任何信息。如果我像这样初始化一个布尔数组:

 bool condition[10] = {true,[5]=true};

I get the output I expect, first and sixth values are true while others are false. But if I write following snippet:

我得到了我期望的输出,第一个和第六个值为真,而其他值为假。但是,如果我编写以下代码段:

 bool condition[10] = {true,condition[5]=true};

I get first, SECOND and sixth values as true. I assume it's some kind of undefined behavior but I'd like someone more knowledgeable than me to explain to me what's going on.

我得到第一个、第二个和第六个值为真。我认为这是某种未定义的行为,但我希望有比我更了解的人向我解释发生了什么。

I'm compiling with extra warning flags, using GCC and "-std=gnu99", and I'm not getting any errors.

我正在使用 GCC 和“-std=gnu99”编译额外的警告标志,但我没有收到任何错误。

采纳答案by ouah

C says that:

C 说:

(C11, 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."

(C11, 6.7.9p23) “初始化列表表达式的评估彼此之间是不确定的,因此任何副作用发生的顺序是不确定的。”

and in C99

在 C99 中

(C99, 6.7.8p23) "The order in which any side effects occur among the initialization list expressions is unspecified."

(C99, 6.7.8p23) “未指定初始化列表表达式中出现任何副作用的顺序。”

That means that the declaration

这意味着声明

    bool condition[10] = {true,condition[5]=true};

can have the same behavior:

可以有相同的行为:

    bool condition[10] = {true, 1};

or as

或作为

    bool condition[10] = {true, 1, [5] = true};

whether condition[5] = trueevaluation is done before or after the 0initialization of the array members.

无论condition[5] = true评估的前面或后面做0阵列成员的初始化。

EDIT: there is a case of unspecified initialization order of array elements in Defect Report #208. The case is different because in the DR example there are two initializers for a single element.

编辑:在缺陷报告 #208 中存在未指定数组元素初始化顺序的情况。情况不同,因为在 DR 示例中,单个元素有两个初始值设定项。

http://www.open-std.org/jtc1/sc22/wg14/www/docs/9899tc1/n32074.htm

http://www.open-std.org/jtc1/sc22/wg14/www/docs/9899tc1/n32074.htm

int a [2] = { f (0), f (1), [0] = f (2) };

It was the intention of WG14 that the call f(0) might, but need not, be made when a is initialized. If the call is made, the order in which f(0) and f(2) occur is unspecified (as is the order in which f(1) occurs relative to both of these). Whether or not the call is made, the result of f(2) is used to initialize a[0].

int a [2] = { f (0), f (1), [0] = f (2) };

WG14 的意图是在 a 初始化时调用 f(0) 可能,但不是必须的。如果进行调用,则 f(0) 和 f(2) 出现的顺序是未指定的(f(1) 相对于这两者的出现顺序也是如此)。无论是否进行调用,f(2) 的结果都用于初始化 a[0]。

回答by Neal

That is a nice little puzzle. I think ouah got it, but more explanation would probably help. I think condition[5]=true is nota designated initializer. It is an expression, which evaluates to true as usual. Since that expression is in the second spot, true gets assigned to to condition[1]. Also, as a side-effect of the expression, condition[5] gets set to true.

这是一个不错的小谜题。我认为 ouah 明白了,但更多的解释可能会有所帮助。我认为 condition[5]=true不是指定的初始值设定项。它是一个表达式,像往常一样评估为真。由于该表达式位于第二个位置,因此将 true 分配给条件 [1]。此外,作为表达式的副作用,condition[5] 被设置为 true。