Friday, 26 January 2018

Differentiate between a structure and union in c

Structure : The Structure, Which is a data structure whose individual elements can differ in type. Thus, a single structure might contain integer elements, floating-point elements and character elements. Pointers, arrays and other structure can also be included as elements with in a structure. The individual structure elements are referred to as members.

Union : Closely associated with the structure is the union, which also contains multiple members. Unlike a structure, however, the members of a union share the same storage area, even though the individual members may differ in type. Thus, a union permits several different data items to be stored in the same portion of the computer’s memory at different times.

Similarities :
1.they can have name optional.
2. They can contain members of varying data types.
3. Both declarations end with Semicolon.
4. Union members can be accessed in the same way as structure members.
5. union can be assigned to another union like Structures.
6. Both Union, Structure variable can be placed to functions.

Differences :
1.The memory size of the structure variable is the sum is the sum of sizes of its member. Whereas Union it is the largest size of its memory.
2.It is the programmers responsibility by using Union to keep track of which type is Currently in use unlike in Structures. Where no member is last.
3. In Structures all members can be initialized where as the union can only be initialized with a value of the time of its first member.
4. Structures can be used in the list of process.

0 comments:

Post a Comment

Advertisement

Find Us On Facebook

C Programming

More

C++ Tutorial

More

Java Tutorial

More

Computer Basics

More

MS Office

More

Database Management

More
Top