Din*_*lla 15 c struct initialization c89 visual-c++
伙计们,我有这个问题:
通常在C99 GCC(cygwin/MinGW/linux)中,C struct中的初始化器有点符号语法.
像这样:
//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"},
};
Run Code Online (Sandbox Code Playgroud)
已经my_memeber_t在头文件中定义为:
struct my_member_t {
struct complex_type * type;
char * name;
int default_number;
void * opaque;
};
Run Code Online (Sandbox Code Playgroud)
我在MSVC 9.0(Visual Studio 2008)中编译linux代码,在cygwin/MinGW上这可以正常工作.
但是cl无法编译这个(因为C99实现很糟糕):error C2059: syntax error : '.'
PROBLEM:
How to rewrite (many) global structs in a way that MSVC(resp C89)can compile it?
致以最诚挚的问候和感谢...
AnT*_*AnT 17
悲惨的C99实施?我不认为VC2008中的C编译器甚至试图实现C99.它可能会借用一些功能,但它实际上是一个C89/90编译器.
只需删除字段名称标签即可
static struct my_member_t my_global_three[] = {
{ NULL, "one"},
{ NULL, "two"},
{ NULL, "three"},
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它很容易,因为原始代码中的初始值设定项的顺序与结构中字段的顺序相同.如果订单不同,您必须在无标签C89/90版本中重新排列它们.
如果它确实是您的 my_member_t,那么您应该将字符串指针声明为const char *或停止使用字符串文字初始化这些成员.
| 归档时间: |
|
| 查看次数: |
10094 次 |
| 最近记录: |