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 word [512/2];
    ULONG blklword[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 word 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 (but not ISO tho´)

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

  2. "Andreas Eibach" <> writes:

    > got some tricky problem here:
    > (at least if I want to write good ISO C code)
    >
    > typedef struct {
    > union
    > {
    > USHORT byte [512];
    > ULONG word [512/2];
    > ULONG blklword[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 word nor lword).
    > How can I solve this and still comply to the ISO C standard?


    You have to change all references to S.byte and P->byte to
    S.raw_u.byte and P->raw_u.byte respectively (where S is any expression
    that denotes a structure object and P is pointer to such an object).

    You can play tricks with macros to try to do this automatically but
    that can be risky and does not aid readability. It's better to bite
    the bullet and changes the usage.

    > Curiously, if I ignore the warning of the first solution, it works a
    > treat anyway (but not ISO tho´)


    Well, yes. This is an option. Is 100% ISO C important enough to do
    this re-write? Obviously it is if you ever move the code to a
    compiler that does not understand unnamed unions, but I don't know how
    common they are.

    --
    Ben.
     
    Ben Bacarisse, Oct 6, 2009
    #2
    1. Advertising

  3. "Andreas Eibach" <> wrote:
    > ...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 word nor lword).
    > How can I solve this and still comply to the ISO C standard?


    Why do you need the struct wrapper?

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

    --
    Peter
     
    Peter Nilsson, Oct 6, 2009
    #3
  4. "Ben Bacarisse" <> wrote:
    > You have to change all references to S.byte and P->byte to
    > S.raw_u.byte and P->raw_u.byte respectively (where S is any expression
    > that denotes a structure object and P is pointer to such an object).


    Thanks, exactly that was the naked truth :)

    > You can play tricks with macros to try to do this automatically but
    > that can be risky and does not aid readability. It's better to bite
    > the bullet and changes the usage.


    Heheh, I like the "bite the bullet" bit.
    You appear to be thinking what I think: CLUMSY stuff, in some way; since you
    always have to name the interim member :)

    Seems I probably cannot go without.
    Thanks for answering, and to all others too.

    > Well, yes. This is an option. Is 100% ISO C important enough to do
    > this re-write? Obviously it is if you ever move the code to a
    > compiler that does not understand unnamed unions, but I don't know how
    > common they are.


    Well, at any rate, it's probably better to be on the safe shore.

    -Andreas
     
    Andreas Eibach, Oct 6, 2009
    #4
  5. "Peter Nilsson" <> wrote:
    > Why do you need the struct wrapper?
    >
    > typedef
    > union
    > {
    > USHORT byte [512];
    > ULONG lword[512/4];
    > } raw_s;


    Simple: it comes from the time when I still had it as a nested struct. :)
    But after getting some really good advice, I moved to union. There are rare
    cases I need them; but. if you want them to use same memory area for bytes +
    (long) words, (which often comes up with that bit-operation stuff) it can
    come in real handy at times.

    -Andreas
     
    Andreas Eibach, Oct 6, 2009
    #5
    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:
    343
  2. Peter Dunker

    union in struct without union name

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

Share This Page