union inside struct - how to get rid of "ISO C" warning?

Discussion in 'C Programming' started by Andreas Eibach, Oct 6, 2009.

  1. Hi there,

    got some tricky problem here:
    (at least if I want to write good ISO C code)

    typedef struct {
    union
    {
    USHORT byte [512];
    ULONG lword[512/4];
    };
    } raw_s;

    Of course, when the compiler is set to accept ISO code only. this will
    output the warning:
    "unnamed structures/unions are not allowed in ISO C".

    So I tried to do

    typedef struct {
    union
    {
    USHORT byte [512];
    ULONG lword[512/4];
    } raw_u;
    } raw_s;

    Now one of my core functions complains that raw_s has no member named 'byte'
    (nor lword)
    How can I solve this and still comply to the ISO C standard?
    Curiously, if I ignore the warning of the first solution, it works a treat
    anyway (well, not ISO tho´;))

    -Andreas
     
    Andreas Eibach, Oct 6, 2009
    #1
    1. Advertising

  2. Andreas Eibach

    Seebs Guest

    On 2009-10-06, Andreas Eibach <> wrote:
    > got some tricky problem here:
    > (at least if I want to write good ISO C code)
    >
    > typedef struct {
    > union
    > {
    > USHORT byte [512];
    > ULONG lword[512/4];
    > };
    > } raw_s;


    Given:

    raw_s foo;

    how would you refer to the "byte" member?

    > typedef struct {
    > union
    > {
    > USHORT byte [512];
    > ULONG lword[512/4];
    > } raw_u;
    > } raw_s;


    > Now one of my core functions complains that raw_s has no member named 'byte'
    > (nor lword)


    Agreed, it doesn't.

    But raw_u does. So you'd write "foo.raw_u.byte".

    (And unless there's other members in raw_s, you don't need raw_s at all.)

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Oct 6, 2009
    #2
    1. Advertising

  3. Andreas Eibach

    James Kuyper Guest

    Seebs wrote:
    > On 2009-10-06, Andreas Eibach <> wrote:
    >> got some tricky problem here:
    >> (at least if I want to write good ISO C code)
    >>
    >> typedef struct {
    >> union
    >> {
    >> USHORT byte [512];
    >> ULONG lword[512/4];
    >> };
    >> } raw_s;

    >
    > Given:
    >
    > raw_s foo;
    >
    > how would you refer to the "byte" member?


    On compilers which support this feature, you would use foo.byte.
    Horribly non-conforming, but it works (on those compilers).
     
    James Kuyper, Oct 6, 2009
    #3
  4. Andreas Eibach

    Seebs Guest

    On 2009-10-06, James Kuyper <> wrote:
    > On compilers which support this feature, you would use foo.byte.
    > Horribly non-conforming, but it works (on those compilers).


    I wasn't sure whether this was a compiler with that feature, or a new
    implementation of something even crazier.

    I actually like that feature and would not object to seeing it added to
    standard C. Breaks no existing code, nicely expressive.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Oct 6, 2009
    #4
  5. Andreas Eibach

    James Kuyper Guest

    Pietro Cerutti wrote:
    > On Mon, 05 Oct 2009 21:54:54 -0400, James Kuyper wrote:
    >
    >> Seebs wrote:
    >>> On 2009-10-06, Andreas Eibach <> wrote:
    >>>> got some tricky problem here:
    >>>> (at least if I want to write good ISO C code)
    >>>>
    >>>> typedef struct {
    >>>> union
    >>>> {
    >>>> USHORT byte [512];
    >>>> ULONG lword[512/4];
    >>>> };
    >>>> } raw_s;
    >>> Given:
    >>>
    >>> raw_s foo;
    >>>
    >>> how would you refer to the "byte" member?

    >> On compilers which support this feature, you would use foo.byte.
    >> Horribly non-conforming, but it works (on those compilers).

    >
    > Does this mean that a maximum of one anonymous union/struct is allowed
    > inside a struct?


    I don't use such a compiler, at least not with that feature turned on,
    so I'm not sure what the rules are. However, multiple anonymous unions
    in the same struct should not be a problem unless they have members with
    the same name, as in your example below:

    > typedef struct {
    > union
    > {
    > char a;
    > int b;
    > };
    > union
    > {
    > long a;
    > double b;
    > };
    > } my_struct;
    >
    > What would my_struct.a mean in this context?
     
    James Kuyper, Oct 6, 2009
    #5
  6. On Oct 6, 12:23 pm, Pietro Cerutti <> wrote:
    > On Mon, 05 Oct 2009 21:54:54 -0400, James Kuyper wrote:
    >
    > > On compilers which support this feature, you would use foo.byte.
    > > Horribly non-conforming, but it works (on those compilers).

    >
    > Does this mean that a maximum of one anonymous union/struct is allowed
    > inside a struct?


    That could be one implementation. Another choice could be that
    multiple anonymous unions are allowed, but that all their members must
    have distict names.

    >
    > typedef struct {
    >         union
    >         {
    >                 char a;
    >                 int  b;
    >         };
    >         union
    >         {
    >                 long a;
    >                 double b;
    >         };
    >
    > } my_struct;
    >
    > What would my_struct.a mean in this context?


    That would be ambiguous and thus an error.

    typedef struct {
    union
    {
    char a;
    int b;
    };
    union
    {
    long c;
    double d;
    };

    } my_other_struct;

    In this case, all members can be named unambiguously.

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Oct 6, 2009
    #6
    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. Ulrich Eckhardt
    Replies:
    3
    Views:
    352
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    898
    Chris Torek
    Apr 26, 2004
  3. Grant Edwards
    Replies:
    3
    Views:
    591
    Grant Edwards
    Aug 11, 2004
  4. David Resnick
    Replies:
    7
    Views:
    495
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F
    Oct 29, 2007
  5. Andreas Eibach
    Replies:
    4
    Views:
    1,123
    Andreas Eibach
    Oct 6, 2009
Loading...

Share This Page