我可以用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);

会有用的.

在c语言中如何使用gcc实现这一点?我有

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++相关问答推荐

Bison解析器转移/减少冲突

位屏蔽对于无符号转换是强制的吗?

SDL 2.0-从数组渲染纹理

C中的指针增量和减量(*--*++p)

在C++中使用函数指针的正确语法

C中函数类型的前向声明

我的程序在收到SIGUSR1信号以从PAUSE()继续程序时总是崩溃()

如何确保在C程序中将包含uft8字符的字符串正确写入MySQL?

在for循环中指向数组开头之前

如何将另一个数组添加到集合中,特别是字符串?

如何在C-函数中混合使用C代码和ASM?

C语言中的外部关键字

为什么realloc函数在此代码中修改变量?

将 struct 数组写入二进制文件时发生Valgrind错误

挥发性语义的形式化理解

将size_t分配给off_t会产生符号转换错误

在C中,为什么这个带有递增整数的main函数从不因溢出而崩溃?

std::malloc/calloc/realloc/free 与纯 C 的 malloc/calloc/realloc/free 有什么不同

C Makefile - 如何避免重复提及文件名

获取 struct 中匿名 struct 的大小