为什么这是合法的C [英] Why is this valid C

查看:97
本文介绍了为什么这是合法的C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这个code来到<一个href=\"http://www.reddit.com/r/programming/comments/r55bo/what_does_0_mean_in_c_stack_overflow/c430cp2\">reddit.我本来以为类型转换会造成这是无效的。

I came across this code on reddit. I would have thought that type conversions would have caused this to be invalid.

int a[3] = { { {1, 2}, {3, 4}, 5, 6 }, {7, 8}, {9}, 10 };

在铛,我得到过多的元素和支撑在标量初始化一些警告。但内容 A [1,7,9]

On clang, I get a few warnings about excessive elements and braces in a scalar initializer. But the contents of a is [1, 7, 9].

这是真正合法的,如果是这样,可能有人解释一下到底是怎么回事呢?

Is this actually legitimate, and if it is, could someone explain what exactly is going on?

推荐答案

则多余的元素将被忽略。有 6.7.8初始化您关心的两个部分。首先,从第17段:

The excess elements are just ignored. There are two parts of 6.7.8 Initialization that you care about. First, from paragraph 17:

每个大括号内的初始化列表有一个相关的当前对象。如果没有指定值为present,当前对象的子对象,以便根据当前对象的类型初始化:增加下标顺序数组元素,在声明顺序结构成员和工会的第一个命名成员

Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.

那一个解释为什么你会得到1,7和9 - 当前对象获取这些括号设置。那么,为什么它不关心群众演员,从第20段:

That one explains why you get 1, 7, and 9 - the current object gets set by those braces. Then as to why it doesn't care about the extras, from paragraph 20:

...只从列表足够初始化被带到占子聚集或含有联合的第一个部件的元件或部件;任何剩余的初始化都留给初始化聚集体的当前子聚集或包含联合是一个部分的下一个元素或部件

... only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.

这篇关于为什么这是合法的C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆