关于C/C++的{0} initializer

名字写的这么绕,其实就是常用的struct initializer的写法{0},老是这么 用,习焉不察矣,今天别人问起来。想起来差了老半天C/C++标准。终于把这 个问题搞清楚了。这里以C99标准为准–—C++标准的相关部分是从C标准里面 抄来的。

很显然,标准里面对不完全的initializer list的行为是有规定的,相关表述 如下(下面引文全部引自C99标准草稿):

[6.7.8.21] If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

也就是说,类似这样:

struct {int a;int b;} aa = {0};

的写法是完全没问题的,并且会把a,b都zeroing(a是从initializer literal 来的,b是上述因为规定的)。

有一个基本常识是,C里面的非复合类型(我自己造的词,那么个意思,大家别 细究了)都可以用0初始化,所以{0}初始化上面例子中类似的结构是没问题的, 有的人的疑问在于如果struct的第一个元素是struct怎么办。标准规定了这种 情况下的行为:

[6.7.8.20] If the aggregate contains elements or members that are aggregates or unions, or if the first member of a union is an aggregate or union, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the first member of the contained union. Otherwise, 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.

这段话很长很绕,简单的说,如果aggregate(这里简单理解为struct,准确的 表述去看标准)的某个元素是aggregate,就按照普通的aggregate初始化规则找 到该aggregate的初始化列表开始的地方,如果是大括号扩着的,就用那个括 号里面的list初始化这个aggregate,否则就从这里开始找到”足够”的条目给 自己来initialize,剩下的给别人用。

以这个例子来说:

struct {struct {int a;int b;} aa;int c;} _a = {1 ,2};

struct的第一个元素是struct,initializer它相应的位置没有{…}这样的东 东,而是个”1”,那它就从这里拿”足够”的initializer来初始化自己,就是把 “1,2”,拿来初始化a,b了。所以结果就是a == {{1,2},0};

这样,对上面这个结构用{0}初始化就很容易理解了。

Date: 2012-05-28 Mon

Author: Ma Tao

Org version 7.8.05 with Emacs version 23


最符合C++标准的写法是 {} 而不是 {0},因为第一个成员可能不是内建类型。对C++,初始化并不全是zeroing,对类是要调用默认构造函数的。

转自http://www.cppblog.com/qingant/archive/2012/05/28/176517.html

在虚拟机中配置VSCode的C/C++环境可以让你在虚拟机中进行C/C++的开发工作。以下是配置步骤: 1. 安装VSCode:首先,在虚拟机中下载并安装Visual Studio Code。你可以从VSCode官方网站上下载适用于你的操作系统的安装包。 2. 安装C/C++扩展:打开VSCode,点击左侧的扩展图标(或按下Ctrl+Shift+X),在搜索框中输入"C/C++",找到并安装Microsoft的C/C++扩展。 3. 安装编译器:在虚拟机中,你需要安装一个C/C++编译器。常用的编译器有GCC和Clang。你可以使用包管理器(如apt-get、yum等)来安装编译器。 4. 配置编译器路径:打开VSCode,点击左下角的设置图标(或按下Ctrl+,),在搜索框中输入"C/C++",找到"C/C++: Edit Configurations (UI)"选项,点击进入编辑配置界面。在"Compiler path"字段中填入你安装的编译器的路径。 5. 创建C/C++项目:在VSCode中,点击左侧的资源管理器图标(或按下Ctrl+Shift+E),右键点击一个文件夹,选择"New Folder"来创建一个新的文件夹作为你的项目目录。然后,点击左侧的源代码管理图标(或按下Ctrl+Shift+G),选择"Initialize Repository"来初始化一个Git仓库。 6. 编写C/C++代码:在项目目录中创建一个新的C/C++源文件(如hello.c),在文件中编写你的C/C++代码。 7. 编译和运行代码:在VSCode中,点击左侧的调试图标(或按下Ctrl+Shift+D),点击顶部的绿色箭头按钮来编译和运行你的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值