Is this legal code?

Discussion in 'C++' started by Ed J, Jun 16, 2004.

  1. Ed J

    Ed J Guest

    I'm trying to port some C++ code from MS Visual Studio 6.0 to a version of
    Code Composer Studio for an embedded application.

    This code compiles under Visual Studio, and lets the program reference x, y,
    and z. Under a Code Composer Studio compiler, the compiler warns: "this
    declaration doesn't declare anything", then gives errors for all subsequent
    references to x, y, and z.

    union{
    struct{
    float x, y, z; // Named element representation
    };
    float v[3]; // Vector component
    };

    My C++ book implies that the struct declaration shouldn't declare any data
    because there is no assigned name, and I believe that is why Code Composer
    is complaining.

    Is this some legal C++ extension that is just not supported by Code
    Composer, or is it a non-standard Visual Studio extension?

    Ed.
     
    Ed J, Jun 16, 2004
    #1
    1. Advertising

  2. "Ed J" <> wrote in message
    news:...
    > I'm trying to port some C++ code from MS Visual Studio 6.0 to a version of
    > Code Composer Studio for an embedded application.
    >
    > This code compiles under Visual Studio, and lets the program reference x,

    y,
    > and z. Under a Code Composer Studio compiler, the compiler warns: "this
    > declaration doesn't declare anything", then gives errors for all

    subsequent
    > references to x, y, and z.
    >
    > union{
    > struct{
    > float x, y, z; // Named element representation
    > };
    > float v[3]; // Vector component
    > };
    >
    > My C++ book implies that the struct declaration shouldn't declare any data
    > because there is no assigned name, and I believe that is why Code

    Composer
    > is complaining.
    >
    > Is this some legal C++ extension that is just not supported by Code
    > Composer, or is it a non-standard Visual Studio extension?
    >
    > Ed.


    It's usually called an anonymous struct, it's not standard C++. Anonymous
    unions on the other hand are standard.

    john
     
    John Harrison, Jun 16, 2004
    #2
    1. Advertising

  3. Ed J wrote:
    > union{
    > struct{
    > float x, y, z; // Named element representation
    > };
    > float v[3]; // Vector component
    > };


    A few points:
    1. Nested types aren't allowed to be declared in anonymous unions (but
    you could work around that with a non-nested type)
    2. At global/namepsace scope the union must be declared static
    3. Although v and x,y,z occupy the same memory space, there is no
    guarnatee the the three values of v are the same values as x,y,z
    (alignment requirements could align the variables differently)


    --
    edA-qa mort-ora-y (Producer)
    Trostlos Records <http://trostlos.org/>

    "What suffering would man know if not for his own?"
     
    edA-qa mort-ora-y, Jun 16, 2004
    #3
  4. Ed J

    JKop Guest

    Ed J posted:

    > union{
    > struct{
    > float x, y, z; // Named element representation
    > };
    > float v[3]; // Vector component
    > };



    This would be exactly like writing:


    int;
    float;


    You're not naming the variable, nor are you defining a union. The following
    would be valid:


    union Cat {
    struct {
    float x, y, z;
    };

    float v[3];
    };

    Here I have defined a union... (I hasten to use the term "template"!)...
    from which I can in the future *declare* variables, as in:

    Cat cat;

    OR, going down another road:

    union {
    struct {
    float x, y ,z;
    };

    float v[3];
    } cat;


    Your original code would only be valid in the following circumstance:

    struct Choc {
    union {
    struct {
    float x, y, z;
    };

    float v[3];
    };
    };


    -JKop
     
    JKop, Jun 17, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Johnson
    Replies:
    3
    Views:
    485
    C Johnson
    Aug 14, 2003
  2. SenderX
    Replies:
    7
    Views:
    381
    Greg Comeau
    Aug 29, 2003
  3. Jim Ford

    Is this legal C code?

    Jim Ford, Jan 26, 2004, in forum: C Programming
    Replies:
    7
    Views:
    359
    James Hu
    Jan 28, 2004
  4. Ancient_Hacker

    Weird bit of code-- legal???

    Ancient_Hacker, Sep 13, 2006, in forum: C Programming
    Replies:
    29
    Views:
    591
    jaysome
    Sep 15, 2006
  5. Marco Jez

    [templates] is this code legal?

    Marco Jez, Sep 18, 2005, in forum: C++
    Replies:
    9
    Views:
    367
    Marco Jez
    Sep 19, 2005
Loading...

Share This Page