views:

2381

answers:

7

I can do this in c++/g++:

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

Then,

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

will work.

How does one do this in c with gcc? I have

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

But I get errors all around, specifically

line 5: warning: declaration does not declare anything
line 7: warning: declaration does not declare anything
+4  A: 

Anonymous unions is a feature of C++ language. C language has no anonymous unions.

Anonymous structs don't exist in neither C nor C++.

The declaration you presented in your question might compile with GCC C++ complier, but it would be just a compiler-specific extension, which has nothing to do with neither standard C nor standard C++.

On top of that, regardless of how you implement it, neither C nor C++ language guarantees that your assertions will hold.

AndreyT
as a sidenote, gcc does support this as an extension, you'll have run gcc in non standard C mode though(the default), or explicittly use -std=gnu99 or similar.
nos
Yes, I know this, and should have mentioned it. It just makes the code look better, and is not hard to fix if it was unportable. In this case, I'm simply writing it for my own use, so it's not a problem. (I'm writing a c raytracer to learn the intricacies of c)
+6  A: 

C does not have anonymous structures or unions. You have to name them:

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

And then you have to use the name when accessing them:

assert(&v.data.xyz[0] == &v.data.individual.x);

In this case, because your top level structure has a single item of type union, you could simplify this:

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

and accessing the data now becomes:

assert(&v.xyz[0] == &v.individual.x);
R Samuel Klatchko
Is there a gcc specific extension that I may use?
The GNU Dialect of C supports anonymous structures and unions.
David Grayson
A: 

Anonymouse unions are nor supported in C.

Also note that if you declare it this way:

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

Doing

Vector3 v;
v.data.xyz[0] = 5;

float foo = v.data.individual.x;

Is an undefined behaviour. You can only access the last assigned union member. In your case, using an union is wrong and bad coding practice as it's dependent on many things that are not specified in the standard (padding...).

In C you will prefer something like this:

typedef struct {
    float v[3];
} Vec3;

And if you don't want to use v[x] you might consider:

#define X(V) ((V).v[0])

Vec3 v;
X(v) = 5.3;
printf("%f\n", X(v));
The standard says that when a member of a union is assigned, the value of the other members is **unspecified**. It also says that the bit representation is shared among the members. This is not undefined behavior. It seems quite clearly defined.
greyfade
The Standard says "When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values." But that does not mean that the value of other members may not be a trap (their compound bytes are not, merely). It says "The value of a structure or union object is never a trap representation, even though the value of a member of the structure or union object may be a trap representation.". Reading from a different member is not undefined behavior per se, but it may.
Johannes Schaub - litb
.. It may if we read a trap representation and then behavior is undefined, as it says in a foot-note (nonnormative) most clearly: "If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.".
Johannes Schaub - litb
+1  A: 

I can do this in GCC without warning

typedef union {
    struct { // human-friendly access
        float x;
        float y;
        float z;
        float w;
    };
    float xyz[3];
    struct { // human-friendly access
        float r;
        float g;
        float b;
        float a;
    };
    float rgb[3];
} Vector4f;

int main()
{
    Vector4f position, normal, color;
    // human-friendly access
    position.x = 12.3f;
    position.y = 2.f;
    position.z = 3.f;
    position.w = 1.f;

    normal.x = .8f;
    normal.y = .9f;
    normal.z = .1f;
    normal.w = 1.f;

    color.r = 1.f;
    color.g = .233f;
    color.b = 2.11f;
    color.a = 1.1f;

    // computer friendly access
    //some_processor_specific_operation(position.vec,normal.vec);
    return 0;
}

C:\>gcc vec.c -Wall

C:\>gcc --version gcc (GCC) 4.4.0 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

afriza
Still uses extensions. Enter `-pedantic` in the command line: "main.cpp:7: warning: ISO C++ prohibits anonymous structsmain.cpp:14: warning: ISO C++ prohibits anonymous structs"
GMan
well, the question is about GCC, not ISO C++ .. good to know what ISO C++ says though.. ; P
afriza
+4  A: 

according to http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

-fms-extensions will enable the feature you (and I) want.

A: 

The GNU dialect of C supports anonymous structs/unions, but by default GCC compiles using some kind of standard C. To use the GNU dialect, put "-std=gnu99" on the command line.

David Grayson
A: 

Unidentified struct members not being ANSI/ISO C99 standard explains this, but I find a funny thing happens, on some ports of GNU C Compiler 2.x.x versions, using undentified struct members works, it finds them, doesn't say stuff like "x is not a member of union\struct y, what is x?", other times, it's the ol' "x is undefined", "x is not a member of struct", hell I swear I saw a "pointer to unknown" once a while back, due to this.

So I, professionally would go with everyone else on this and just ether give the struct\union member a identifier, or in the case of UNIONs, carefully rearrange the code so the union ends up an identified member of a identified structure and the members that were embedded in the unidentified structure of the original union, become members of the identified structure and are carefully used with the identified union member. But in those cases were the latter method would not be a workable substitute, I would just give the annoynous structure an identifier and move on.

DLCJ