Re: Make out of Structure and Union Hi,
Let me explain u about Structure and Union
Structure
=======
Structure is a collection of elements having different datatypes
For eg..
Struct a
{
int b[5]; //->Memory allocated(10)
char c;//->Memory allocated(1)
};
Structure ends with semicolon...
Here the declaration tells that
'a'->Structure Variable
b[5]->b is a structure element that can hold 5-elements of same datatype[int]..(i.e., it can store 5-Integer Element)
c->character element..
For b[5]->10-bytes are allocated,c->'1-byte is allocated.
Totally for structure variable 'a'->11-bytes are allocated..
For each element separate memory is allocated..
But in Union,
Union c
{
int b;
char c;
};
Here it takes the variable of largest datatype and assign that single memory for all union elements..
i.e.,For union variable 'c' it allocates the total of 2-bytes both for element 'b' and 'c'.(i.e.,Refer same Memory location ,No individual memory for each variable)
This is the main difference between Union and Structure. |