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

A

Andreas Eibach

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
 
B

Ben Bacarisse

Andreas Eibach said:
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.
 
P

Peter Nilsson

Andreas Eibach said:
...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;
 
A

Andreas Eibach

Ben Bacarisse said:
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
 
A

Andreas Eibach

Peter Nilsson said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top