Sie sind auf Seite 1von 5

# DIFFERENCE BETWEEN STRUCTURE AND UNION

## The difference between structure and union in c are:

1. union allocates the memory equal to the maximum memory required by
the member of the union but structure allocates the memory equal to the
total memory required by the members.
2. In union, one block is used by all the member of the union but in case of
structure, each member have their own memory space
Difference in their Usage:
While structure enables us treat a number of different variables stored at
different in memory , a union enables us to treat the same space in memory
as a number of different variables. That is a Union offers a way for a section
of memory to be treated as a variable of one type on one occasion and as a
different variable of a different type on another occasion.
There is frequent requirement while interacting with hardware to access
access a byte or group of bytes simultaneously and sometimes each byte
individually. Usually union is the answer.
=======Difference With example*****
Lets say a structure containing an int, char and float is created and a union
containing int char float are declared. struct TT{ int a; float b; char c; } Union
UU{ int a; float b; char c; }
sizeof TT(struct) would be >9 bytes (compiler dependent-if int,float, char are
taken as 4,4,1)
sizeof UU(Union) would be 4 bytes as supposed from above.
If a variable in double exists in union then the size of union and struct would
be 8 bytes and cumulative size of all variables in struct.

Defining a Union
Union can be defined by the keyword union.

union myUnion{
int var1;
long var2;
};
Here we have defined a union with the name myUnion and it has two members i.e. var1 of
type int and var2 of type long

## Declaring the Union

We can declare the union in various ways. By taking the above example we can declare the
above defined union as.

union myUnion{
int var1;
long var2;
}newUnion;
So newUnion will be the variable of type myUnion. We can also declare the union as

myUnion newUnion;

## Initializing the Union

We can initialize the union in various ways. For example

union myUnion{
int var1;
long var2;
}newUnion={10.5};
or we can initialize it as

newUnion.var1= 10;

In later stages we can also initialize the var2 as well but this will over write the var1 value.
Normally when we declare the union it is allocated the memory that its biggest member can
occupy. So here in our example newUnion will occupy the memory which a long type
variable can occupy.

## Whats the difference between a STRUCT and a UNION in

C++?
Heres an example!
A C++ struct will make sure to allocate space for each and every member in the
struct.

struct VertexStruct
{
float x,y,z;
};

## When you create one, using a line of code like:

VertexStruct vertexstruct;
This is what VertexStruct object will look like in memory.

vertexstruct
___________________
|
|
|
|
|
|
|
|
|_____|_____|_____|
x
y
z
So if we do

vertexstruct.x = 50;

vertexstruct.y = 10;
vertexstruct.z = 2;
We get this picture in memory:

vertexstruct
___________________
|
|
|
|
| 50 | 10 | 2 |
|_____|_____|_____|
x
y
z
This is natural and completely what youd expect.
Contrast STRUCT vs UNION
Now, CONTRAST THAT with a UNION. A UNION uses the SAME EXACT PIECE OF
MEMORY for __ALL__ of the members inside of it.
This is kind of counter-intuitive and weird.
An example:

union VertexUnion
{
float x,y,z;
};

## When you create one, using a line of code like:

VertexUnion vertexunion;
You get this picture in memory:

vertexunion
_______
|
|
|
|
|_____|

x
y
z
The above union effectively provides 3 ways by which to access that same single
piece of memory (x, y and z).
So if you wrote code like:

vertexunion.x = 10;
vertexunion.y = 2;
vertexunion.z = 5;

## You get this picture in memory:

vertexunion
_______
|
|
| 5 |
|_____|
x
y
z
(Last x, y and z all refer to the same piece of memory. Since the last value assigned
there is 5, ALL OF x, y and z have the exact same value).
* Note that the sizeof( any_union ) ends up being the
sizeof( largest_member_in_union ).