What is the differnce between Structure and Union?

Thursday, September 1st, 2011

The difference between structure and union is,
1. The amount of memory required to store a structure variable is the sum of the size of all the members.
On the other hand, in case of unions, the amount of memory required is always equal to that required by its largest member.

2. In case of structure, each member have their own memory space but In union, one block is used by all the member of the union.

Detailed Example:

1
2
3
4
5
6
struct stu
{
	char c;
	int l;
	float p;
};
1
2
3
4
5
6
union emp
{
	char c;
	int l;
	float p;
};

In the above example size of the structure stu is 7 and size of union emp is 4.

Avatar of chitra

Author Name :
chitra

Total : 0 Comment


Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: