使用匿名结构/联合编译 C 代码
出现了如何使用匿名结构或联合编译 C 代码的问题,如C 具有使用联合的匿名字段。在 C 中,尝试使用包含匿名联合的命名结构创建类似的结构会导致编译错误。
错误消息表明匿名联合和结构字段未在结构声明中声明。要在 C 中启用此功能,需要使用 -fms-extensions 编译器标志。
使用 -fms-extensions 修改代码
<code class="c">#include <stdio.h> #include <assert.h> typedef struct { union { float x, y, z; } xyz; } Vector3; int main() { Vector3 v; assert(&v.xyz.x == &v.x); assert(&v.xyz.y == &v.y); assert(&v.xyz.z == &v.z); return 0; }</code>
使用此修改后,代码将成功编译,并且断言将通过,确认联合体成员和结构体字段的地址是等效的。
以上是如何使用匿名结构或联合编译 C 代码?的详细内容。更多信息请关注PHP中文网其他相关文章!