如何重写C-结构指定初始化为C89(RESP MSVC C编译器) [英] How to rewrite C-struct designated initializers to C89 (resp MSVC C compiler)

查看:175
本文介绍了如何重写C-结构指定初始化为C89(RESP MSVC C编译器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有这样的问题:

guys, I've this problem:

通常,在C99 GCC(Cygwin的/ MinGW的/ Linux的),对于在C结构初始化点表示法语法。结果
像这样的:

Normally in C99 GCC (cygwin / MinGW / linux), there is dot-notation syntax for initializers in C struct.
Like this:

//HELP ME HOW TO REWRITE THIS (in most compact way) to MSVC
static struct my_member_t my_global_three[] = {
    {.type = NULL, .name = "one"},
    {.type = NULL, .name = "two"},
    {.type = NULL, .name = "three"},
};

my_memeber_t 在头文件中定义为:

struct my_member_t {
    struct complex_type * type;
    char * name;
    int default_number;
    void * opaque;
};


我在编译的 MSVC 9.0 (Visual Studio 2008中),在Cygwin / MinGW的该工程确定的linux code。结果
CL 无法编译这个(因为惨C99实现):错误C2059:语法错误:。


I'm compiling linux code in MSVC 9.0 (Visual Studio 2008), on cygwin/MinGW this works ok.
BUT cl is unable to compile this (because of miserable C99 implementation): error C2059: syntax error : '.'

问题:结果
如何重写的方式(很多)的全球结构的MSVC
(RESP C89) 可以编译它?

有关建议最好的问候和感谢...

Best regards and thanks for suggestions...

推荐答案

惨C99实现?我不认为C编译器VC2008甚至尝试实现C99。它可能借用一些功能,但它确实是一个C89 / 90的编译器。

Miserable C99 implementation? I don't think that C compiler in VC2008 even tries to implement C99. It might borrow some features, but it is really a C89/90 compiler.

刚落,现场名称标签

static struct my_member_t my_global_three[] = {
    { NULL, "one"},
    { NULL, "two"},
    { NULL, "three"},
};

在此情况下,它是很容易的,因为在初始化的原code中的次序是一样的,在结构域的顺序。如果订单是不同的,你必须重新排列在无标签C89 / 90版本。

In this case it is easy, since the order of the initializers in the original code is the same as the order of fields in the struct. If the order was different, you'd have to rearrange them in the tagless C89/90 version.

如果这是真的的 my_member_t ,那么应该声明字符串指针为为const char * 或停止初始化这些成员字符串。

And if it's really your my_member_t, then you should either declare the string pointer as const char * or stop initializing these members with string literals.

这篇关于如何重写C-结构指定初始化为C89(RESP MSVC C编译器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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