On 2006-05-29 13:23, Vijay wrote:
Can you tell me what is the diffrence between structure and union.
In a struct the members are placed on separate memory locations so in
the following example one and two does not occupy the same memory.
struct s { int i1; int i2; };
In memory it could look like this
|--------|--------|
<--i1--> <--i2-->
With a union on the other hand the members occupy the same memory
location like this:
union u { int i1; int i2; };
|--------|--------|
<--i1-->
<--i2-->
That means if you change the value of i1 you also change the value of
i2, this makes unions dangerous. Add to this the fact that the alignment
of the members in unspecified and may differ between architectures and
you quickly run into portability-issues. In short, don't use unions if
you are not 100% sure of what you are doing.
Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup