What is the differnce between Structure and Union?

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.

Chitra
Chitra

6 thoughts on “What is the differnce between Structure and Union?

  1. Because of giving example I clearly understand difference between structure & union about memory. Nice!

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in