Re: Why not unions
- From: "Alex Fraser" <me@xxxxxxxxxxx>
- Date: Wed, 27 Jul 2005 23:26:37 +0100
"Rob Thorpe" <robert.thorpe@xxxxxxxxxxxx> wrote in message
news:1122490853.570267.241310@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[snip]
> In C there are two main uses of unions:
> 1. To save space by using a single location to store many types of
> data.
[snip]
> union bar {
> int x;
> double y;
> }
> struct foo {
> enum thing what_it_is;
> union bar b;
> }
>
> foo.what_it_is describes what is in the union b, it could have values
> DOUBLE or INT for example.
>
> It's only worth making this a union if we need the space. If we don't
> then we could use:
>
> struct foo {
> enum thing what_it_is;
> int x;
> double y;
> char *z;
> }
>
> Which is simpler and easier to check.
Why do you think that?
FWIW, I would generally use an "unnamed" (not sure if that is the correct
term) union type declared inside the struct:
struct foo {
enum thing what_it_is;
union {
int x;
/* etc */
} b;
};
Alex
.
- Follow-Ups:
- Re: Why not unions
- From: Rob Thorpe
- Re: Why not unions
- References:
- Why not unions
- From: Shwetabh
- Re: Why not unions
- From: Rob Thorpe
- Why not unions
- Prev by Date: Re: Are programmers like this in the real world?
- Next by Date: Re: Question about minimal requirements in programming
- Previous by thread: Re: Why not unions
- Next by thread: Re: Why not unions
- Index(es):
Relevant Pages
|