gpt4 book ai didi

c++ - 如何使用匿名结构/union 编译 C 代码?

转载 作者:IT老高 更新时间:2023-10-28 12:14:09 25 4
gpt4 key购买 nike

我可以在 c++/g++ 中做到这一点:

struct vec3 { 
union {
struct {
float x, y, z;
};
float xyz[3];
};
};

那么,

vec3 v;
assert(&v.xyz[0] == &v.x);
assert(&v.xyz[1] == &v.y);
assert(&v.xyz[2] == &v.z);

会起作用的。

如何用 gcc 在 c 中做到这一点?我有

typedef struct {
union {
struct {
float x, y, z;
};
float xyz[3];
};
} Vector3;

但是我到处都遇到错误,特别是

line 5: warning: declaration does not declare anything
line 7: warning: declaration does not declare anything

最佳答案

根据 http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

-fms-extensions 将启用您(和我)想要的功能。

关于c++ - 如何使用匿名结构/union 编译 C 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com