Unions in structures

Discussion in 'C Programming' started by Morris Dovey, Mar 16, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    blockstack wrote:
    >
    > I recently noticed (I'm new to the language) that when using a union
    > type as a component of a structure, you don't need a union-tag.
    >
    > struct foo
    > {
    > char *ptr;
    > union
    > {
    > int i;
    > float f;
    > char c;
    > };
    > };
    >
    > I have never read that a union type reference is optional in any
    > case.
    > Is this specific to the compiler, or part of the standard?


    A footnote to Eric's response:

    This convention produces headaches when/if struct foo is expanded
    to:

    struct foo
    { char *ptr;
    unsigned i;
    union
    { int i;
    float f;
    char c;
    };
    };

    struct foo x;
    x.i = 7; /* Which i in x? */

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
    Morris Dovey, Mar 16, 2008
    #1
    1. Advertising

  2. Morris Dovey

    blockstack Guest

    I recently noticed (I'm new to the language) that when using a union
    type as a component of a structure, you don't need a union-tag.

    struct foo
    {
    char *ptr;
    union
    {
    int i;
    float f;
    char c;
    };
    };

    I have never read that a union type reference is optional in any
    case.
    Is this specific to the compiler, or part of the standard?
    blockstack, Mar 16, 2008
    #2
    1. Advertising

  3. Morris Dovey

    Eric Sosman Guest

    blockstack wrote:
    > I recently noticed (I'm new to the language) that when using a union
    > type as a component of a structure, you don't need a union-tag.
    >
    > struct foo
    > {
    > char *ptr;
    > union
    > {
    > int i;
    > float f;
    > char c;
    > };
    > };
    >
    > I have never read that a union type reference is optional in any
    > case.
    > Is this specific to the compiler, or part of the standard?


    A union does not need to have a union tag, and a struct
    does not need to have a struct tag. But I think you may be
    mixing up the terminology!

    - A "struct tag" is an identifier that can be combined
    with the keyword `struct' to form a name for a specific
    struct type. In your example, `foo' is a struct tag
    and `struct foo' is the name of the type. If the `foo'
    were absent, the struct would have no tag and there
    would be no way to write a name for its type elsewhere
    in the program.

    - Similarly, a "union tag" is an identifier that can be
    combined with the keyword `union' to form a name for a
    specific union type. In your example, the union has no
    tag and there is no way to write the name of the union's
    type. If the identifier `bar' appeared right after the
    `union' keyword, `bar' would be the union tag and
    `union bar' would be the name of the type.

    - Almost every element contained in a struct or union (the
    lone exception is not important here) must have an element
    name. In your example, `i', `f', and `c' are the names
    of the elements of the union. `ptr' is the name of one
    element of the struct, but the other element (the union)
    has no name. This is a compile-time error for which the
    compiler is required to produce a diagnostic message.

    So, why do you get no error message? Probably because you
    are not using a C compiler, but a C-with-extras compiler that
    accepts a C-like language that isn't really C. The gcc compiler
    is like that in its default mode of operation; command-line flags
    like `-ansi -pedantic' can make it conform more strictly to the
    actual definition of C.

    --
    Eric Sosman
    lid
    Eric Sosman, Mar 16, 2008
    #3
  4. Morris Dovey

    blockstack Guest

    On Mar 16, 12:58 pm, Eric Sosman <> wrote:
    > blockstack wrote:
    > > I recently noticed (I'm new to the language) that when using a union
    > > type as a component of a structure, you don't need a union-tag.

    >
    > > struct foo
    > > {
    > > char *ptr;
    > > union
    > > {
    > > int i;
    > > float f;
    > > char c;
    > > };
    > > };

    >
    > > I have never read that a union type reference is optional in any
    > > case.
    > > Is this specific to the compiler, or part of the standard?

    >
    > A union does not need to have a union tag, and a struct
    > does not need to have a struct tag. But I think you may be
    > mixing up the terminology!
    >
    > - A "struct tag" is an identifier that can be combined
    > with the keyword `struct' to form a name for a specific
    > struct type. In your example, `foo' is a struct tag
    > and `struct foo' is the name of the type. If the `foo'
    > were absent, the struct would have no tag and there
    > would be no way to write a name for its type elsewhere
    > in the program.
    >
    > - Similarly, a "union tag" is an identifier that can be
    > combined with the keyword `union' to form a name for a
    > specific union type. In your example, the union has no
    > tag and there is no way to write the name of the union's
    > type. If the identifier `bar' appeared right after the
    > `union' keyword, `bar' would be the union tag and
    > `union bar' would be the name of the type.
    >
    > - Almost every element contained in a struct or union (the
    > lone exception is not important here) must have an element
    > name. In your example, `i', `f', and `c' are the names
    > of the elements of the union. `ptr' is the name of one
    > element of the struct, but the other element (the union)
    > has no name. This is a compile-time error for which the
    > compiler is required to produce a diagnostic message.
    >
    > So, why do you get no error message? Probably because you
    > are not using a C compiler, but a C-with-extras compiler that
    > accepts a C-like language that isn't really C. The gcc compiler
    > is like that in its default mode of operation; command-line flags
    > like `-ansi -pedantic' can make it conform more strictly to the
    > actual definition of C.
    >
    > --
    > Eric Sosman
    >


    Thanks,
    Using the option --ansi=pedantic did cause an error. I used union-tag
    and union-type-reference synonymously as I thought both were
    identifiers, but I see how I needed to make the distinction.
    The reason I was asking was because I was reading an old K&R C book
    from the 80s in which they were describing the previous code as a
    union variation rather than a structure. And then I received no error,
    so I wondered if this was part of the standard.
    But it is not standard C. It does seem to make things easier though.
    blockstack, Mar 16, 2008
    #4
  5. blockstack <> writes:
    > I recently noticed (I'm new to the language) that when using a union
    > type as a component of a structure, you don't need a union-tag.
    >
    > struct foo
    > {
    > char *ptr;
    > union
    > {
    > int i;
    > float f;
    > char c;
    > };
    > };
    >
    > I have never read that a union type reference is optional in any
    > case.
    > Is this specific to the compiler, or part of the standard?


    It's specific to the compiler. As far as the standard is concerned,
    the above is an error requiring a diagnostic. Some compilers allow
    anonymous unions as an extension.

    In particular, gcc supports this. gcc's "-pedantic" option will cause
    it to issue a warning.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 16, 2008
    #5
  6. Morris Dovey

    Eric Sosman Guest

    blockstack wrote:
    > On Mar 16, 12:58 pm, Eric Sosman <> wrote:
    >> blockstack wrote:


    [concerning a struct containing a union element with no identifier]

    > [...] And then I received no error,
    > so I wondered if this was part of the standard.
    > But it is not standard C. It does seem to make things easier though.


    It can be handy, and several compilers allow it as an
    extension to Standard C. The uses I've seen, though, have
    all been of a somewhat dubious nature, along the lines of

    struct thing {
    struct {
    unsigned int readyFlag : 1;
    unsigned int willingFlag : 1;
    unsigned int ableFlag : 1;
    }; /* no name */
    unsigned int flags;
    #define READY_FLAG 0x1
    #define WILLING_FLAG 0x2
    #define ABLE_FLAG 0x4
    };

    The idea is that you can access the flags individually:

    struct thing x;
    x.readyFlag = 1;
    x.willingFlag = 0;
    x.ableFlag = 1;

    .... or en masse:

    struct thing y;
    y.flags = READY_FLAG | ABLE_FLAG;

    The "dubious" part here is that the compiler has a lot
    of freedom in how it arranges the bit fields in a struct.
    On some compilers (that allow no-name elements), the code
    above may work as intended. But on others, the macro
    values should be 0x80000000, 0x40000000, and 0x20000000,
    or maybe 0x01000000, 0x00010000, and 0x00000100, or maybe
    something else altogether. So it *looks* like a convenient
    dodge, but may wind up relying on the internals of the
    compiler. The result certainly isn't portable, not even
    portable among compilers allowing no-name elements.

    --
    Eric Sosman
    lid
    Eric Sosman, Mar 16, 2008
    #6
  7. Morris Dovey

    Ben Pfaff Guest

    Eric Sosman <> writes:

    > The uses I've seen, though, have
    > all been of a somewhat dubious nature, along the lines of
    >
    > struct thing {
    > struct {
    > unsigned int readyFlag : 1;
    > unsigned int willingFlag : 1;
    > unsigned int ableFlag : 1;
    > }; /* no name */
    > unsigned int flags;
    > #define READY_FLAG 0x1
    > #define WILLING_FLAG 0x2
    > #define ABLE_FLAG 0x4
    > };


    I think that "struct thing" should be "union thing".
    --
    "When I have to rely on inadequacy, I prefer it to be my own."
    --Richard Heathfield
    Ben Pfaff, Mar 16, 2008
    #7
  8. Morris Dovey

    Eric Sosman Guest

    Ben Pfaff wrote:
    > Eric Sosman <> writes:
    >
    >> The uses I've seen, though, have
    >> all been of a somewhat dubious nature, along the lines of
    >>
    >> struct thing {
    >> struct {
    >> unsigned int readyFlag : 1;
    >> unsigned int willingFlag : 1;
    >> unsigned int ableFlag : 1;
    >> }; /* no name */
    >> unsigned int flags;
    >> #define READY_FLAG 0x1
    >> #define WILLING_FLAG 0x2
    >> #define ABLE_FLAG 0x4
    >> };

    >
    > I think that "struct thing" should be "union thing".


    Age has not withered nor custom staled the correctness
    of your thought. Thanks.

    --
    Eric Sosman
    lid
    Eric Sosman, Mar 16, 2008
    #8
  9. "Eric Sosman" <> schreef in bericht
    news:...
    > Age has not withered nor custom staled the correctness
    > of your thought. Thanks.


    I somehow get the urge to print this out in big letters and hang it on the
    wall. But eh what does it mean?

    (literally)
    Serve Laurijssen, Mar 17, 2008
    #9
  10. Morris Dovey

    Eric Sosman Guest

    [OT] Re: Unions in structures

    Serve Laurijssen wrote:
    >
    > "Eric Sosman" <> schreef in bericht
    > news:...
    >> Age has not withered nor custom staled the correctness
    >> of your thought. Thanks.

    >
    > I somehow get the urge to print this out in big letters and hang it on
    > the wall. But eh what does it mean?


    It's a parody of a famous English-language playwright.

    http://www.phrases.org.uk/meanings/24800.html

    .... and it means I was embarrassed for my blunder, and
    tried to distract attention from it even in the act of
    acknowledging it.

    --
    Eric Sosman, Mar 17, 2008
    #10
    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. Neil Zanella
    Replies:
    9
    Views:
    416
    Jeffrey D. Smith
    Oct 16, 2003
  2. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  3. Jason Curl

    Unions and structures implementation in C

    Jason Curl, Oct 12, 2005, in forum: C Programming
    Replies:
    4
    Views:
    799
    Default User
    Oct 12, 2005
  4. Replies:
    1
    Views:
    281
    Michael Mair
    Apr 1, 2006
  5. Replies:
    20
    Views:
    704
    Richard
    Aug 10, 2007
Loading...

Share This Page