Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > union inside struct - how to get rid of "ISO C" warning?

Reply
Thread Tools

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

 
 
Andreas Eibach
Guest
Posts: n/a
 
      10-06-2009
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

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-06-2009
"Andreas Eibach" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      10-06-2009
"Andreas Eibach" <(E-Mail Removed)> 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
 
Reply With Quote
 
Andreas Eibach
Guest
Posts: n/a
 
      10-06-2009


"Ben Bacarisse" <(E-Mail Removed)> 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

 
Reply With Quote
 
Andreas Eibach
Guest
Posts: n/a
 
      10-06-2009

"Peter Nilsson" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
union inside struct - how to get rid of "ISO C" warning? Andreas Eibach C Programming 5 10-06-2009 10:39 AM
Getting sizeof an anonymous struct declared inside a union David Resnick C Programming 7 10-29-2007 05:54 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM



Advertisments