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 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
 
S

Seebs

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
 
J

James Kuyper

Seebs said:
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).
 
S

Seebs

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
 
J

James Kuyper

Pietro said:
Seebs said:
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:
 
B

Bart van Ingen Schenau

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top