有没有一种方法可以使用结构成员的名称来初始化 const 实例
typedef struct {
int i1;
int i2;
int i3;
} info_t;
//- GCC
const info_t info = {
.i1 = 1,
.i2 = 2
}
//- VS
const info_t info = {1,2,0);
GCC 允许这种方便的方式,但 Visual Studio 会导致错误 C2143“语法错误:之前缺少 }。”...GCC 还允许省略成员(参见示例:info.t3 不是设置)
有谁知道一种简单的方法来通过 VS 的解决方法来生成兼容且易于阅读的代码?
最佳答案
它被称为指定初始值设定项,是在 C99 中引入的。
但是 Visual Studio 目前不支持 C99,所以,不,你不能在 Visual Studio 中执行此操作,你必须坚持使用 C89 方式:
const info_t info = {1,2,0);
但是,根据MSDN和 Infoq在Visual Studio的路线图上,Visual Studio 2013 RTM中将会有一些对C99的支持,这个功能就是其中之一。
关于c - (MS Visual Studio) c 中按成员名称初始化 const typedef 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429902/