Home >Backend Development >C++ >How to Access Anonymous Struct and Union Members as Arrays in C with GCC?
Compiling C Code with Anonymous Structs and Unions
When working with anonymous structs or unions in C , the syntax is straightforward and allows for accessing members like arrays. However, when trying to replicate this functionality in C using GCC, one may encounter compilation errors.
In C, to define an anonymous struct or union, it is necessary to declare it within a parent struct or union. However, by default, GCC does not have support for accessing anonymous members as arrays.
To enable the desired behavior, the -fms-extensions compiler flag should be used. This flag enables Microsoft Visual C compatibility extensions, including the ability to access anonymous struct and union members as arrays.
With the -fms-extensions flag enabled, the code snippet given in the question can be used in C with GCC, and the following assertions will hold true:
assert(&v.xyz[0] == &v.x); assert(&v.xyz[1] == &v.y); assert(&v.xyz[2] == &v.z);
The above is the detailed content of How to Access Anonymous Struct and Union Members as Arrays in C with GCC?. For more information, please follow other related articles on the PHP Chinese website!