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

  • Follow


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 

0
Reply aeibach1 (47) 10/6/2009 12:53:47 AM

In <7ivim7F32pd7cU1@mid.uni-berlin.de>, Andreas Eibach wrote:

<snip>
 
> 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?

Build up logically.

#define BASE_ARRAY_SIZE 512
#define BYTES_PER_LWORD 4

union raw_u_
{
  unsigned short byte[BASE_ARRAY_SIZE];
  unsigned long lword[BASE_ARRAY_SIZE / BYTES_PER_LWORD];
};

typedef union raw_u_ raw_u;


Then, if you need to wrap that up in a struct:

struct raw_s_
{
  raw_u u;
};

typedef struct raw_s_ raw_s;


Now you can define an instance of this structure, eg:

raw_s my_s = {0};

and access its members like this:

my_s.u.byte[1] = 12;


There are less wordy ways to build the structure you want, but I've 
always found that if you're having trouble with definitions you can 
normally sort them out most easily by breaking everything down into 
its component parts and building them up one level at a time.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
0
Reply rjh (10789) 10/6/2009 1:05:26 AM


On 2009-10-06, Andreas Eibach <aeibach@mail.com> 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 / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply usenet-nospam (2199) 10/6/2009 1:11:32 AM

Seebs wrote:
> On 2009-10-06, Andreas Eibach <aeibach@mail.com> 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).
0
Reply jameskuyper (5138) 10/6/2009 1:54:54 AM

On 2009-10-06, James Kuyper <jameskuyper@verizon.net> 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 / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply usenet-nospam (2199) 10/6/2009 2:36:26 AM

On Mon, 05 Oct 2009 21:54:54 -0400, James Kuyper wrote:

> Seebs wrote:
>> On 2009-10-06, Andreas Eibach <aeibach@mail.com> 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?

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

What would my_struct.a mean in this context?
0
Reply gahr (143) 10/6/2009 10:23:53 AM

Pietro Cerutti wrote:
> On Mon, 05 Oct 2009 21:54:54 -0400, James Kuyper wrote:
> 
>> Seebs wrote:
>>> On 2009-10-06, Andreas Eibach <aeibach@mail.com> 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?
0
Reply jameskuyper (5138) 10/6/2009 10:31:36 AM

On Oct 6, 12:23=A0pm, Pietro Cerutti <g...@gahr.ch> 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 {
> =A0 =A0 =A0 =A0 union
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 char a;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int =A0b;
> =A0 =A0 =A0 =A0 };
> =A0 =A0 =A0 =A0 union
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 long a;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 double b;
> =A0 =A0 =A0 =A0 };
>
> } 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
0
Reply bart855 (270) 10/6/2009 10:39:16 AM

7 Replies
31 Views

(page loaded in 0.457 seconds)

Similiar Articles:





7/11/2012 11:01:01 PM


Reply: