Q:

Unions versus structures in C

The idea behind this question is to understand the deeper concepts of using union and using it in different way so as to save memory.. My question to all is--
let's say there is a structure
struct strt
{
   float f;
   char c;
   int a;
}

and the same structure represented in union 
union unin
{
   float f;
   char c;
   int a;
}

If I allocate values to structure members one after another and then print them, it gets
printed. But in case of union it doesn't happen, some overwriting is being done..
So I need to find out a method which can store the values of f,c,a using union and then I can print the same. (Apply any operations or anything..) but I am in search of this technique.. Can anybody out there guide me or give me any idea?

A:

If you were to look at how a struct stores its values, it would be something like this:
|0---1---2---3---|4---|5---6---7---8---|
|ffffffffffffffff|    |                | <- f: Where your float is stored
|                |cccc|                | <- c: Where your char is stored
|                |    |aaaaaaaaaaaaaaaa| <- a: Where your int is stored

So when you change the value of f, you are actually changing bytes 0-3. When you change your char, you are actually changing byte 4. When you change your int, you are actually changing bytes 5-8.
If you now look at how a union stores its values, it would be something like this:
|0---1---2---3---|
|ffffffffffffffff| <- f: where your float is stored
|cccc------------| <- c: where your char is stored
|aaaaaaaaaaaaaaaa| <- a: where your int is stored

So now, when I change the value of f, I am changing bytes 0-3. Since c is stored in byte 0, when you change f, you also change c and a! When you change c, you're changing part of f and a - and when you change a, you're changing c and f. That's where your "overwriting" is happening. When you pack the 3 values into the one memory address, you're not "saving space" at all; you're just creating 3 different ways of looking at and changing the same data. You don't really have an int, a float, and a char in that union - at the physical level, you've just got 32 bits, which could be viewed as an int, a float, or a char. Changing one is meant to change the others. If you don't want them to change each other, then use a struct.
This is why gcc tells you that your struct is 9 bytes long, while your union is only 4 - it's not saving space - it's just that structs and unions are not the same thing.

A:

I think you misunderstand the purpose of a union.
A union, as the name suggests, defines a structure where all of its members occupy the same memory space. Whereas a struct places each of its members in separate memory in a single, contiguous area.
With your union, when you write:
union foo;
foo.c = 3;

Then foo.a and foo.f will both be changed. This is because .a, .c, and .f are stored at the same memory location. Thus, each member of a union is a different "view" of the same memory. This does not happen with a struct because all of the members are distinct and separate from each other.
There is no way around this behavior because it's intentional.

A:

I think you are misunderstanding Unions.

The idea behind using unions is toe save memory...

yes, that's one reason

... and get result equivalent to structure...

no
it's not equivalent. They looks similar in source code, but it is a completely different thing. Like apples and airplanes.
Unions are a very, very low level construct that allows you to see a piece of memory as if storing any of its "members", but you only can use one at a time. Even the use of the word "member" is extremely misleading. They should be called "views" or something, not members.
When you write:
union ABCunion
{
    int a;
    double b;
    char c;
} myAbc;

You are saying: "take a piece of memory big enough for the biggest among an int, a char and a double, and lets call it myAbc.
In that memory, now you can store either an int, or a double, or a char. If you store an int, and then store a double, the int is gone forever.
What's the point then?
There are two major uses for Unions.
a) Discriminated storage
That's what we did above. I pick a piece of memory and I give it different meanings depending on context. Sometimes the context is explicit (you keep some variable that indicates what "kind" of variable you stored), and sometimes it can be implicit (based of the section of code, you can tell which one must be in use). Either way, the code needs to be able to figure it out, or you won't be able to do anything sensible with the variable.
A typical (explicit) example would be:
struct MyVariantType
{
    int typeIndicator ;  // type=1 -> It's an int, 
                         // type=2 -> It's a  double, 
                         // type=3 -> It's a  char
    ABCunion body;
};

For example, VB6's "Variants" are Unions not unlike the above (but more complex).
b) Split representation
This is sometimes useful when you need to be able to see a variable as either a "whole" or as a combination of parts. It's easier to explain with an example:
union DOUBLEBYTE
{
    struct
    {
        unsigned char a;
        unsigned char b;
    } bytes;
    short Integer;        
} myVar;

Here's a short int "unioned" with a pair of bytes. Now, you can view the same value as either a short int (myVar.Integer), or you can just as easily study the individual bytes that make part of the value (myVar.bytes.a and myVar.bytes.b).
Note that this second use is not portable (I'm pretty sure); meaning that it's not guaranteed to work across different machine architectures; but this use is absolutely essential for the kind of tasks for which C was designed (OS implementation).

