Difference Between Similar Terms and Objects

Difference Between Structure and Union

Both the structure and union are user-defined data types in C Language and are conceptually the same, however, they still are different in some ways like the way memory is allocated to their members. They are declared similarly but they function differently. They allow the user to combine different data types under a single name. While separate memory location is used for each member when declaring structure variables, the different members of a union variable share the same memory location. Let’s study the difference between structure and union is detail.

Difference Between Structure and Union

What is Structure?

A structure is just another user-defined data type in C that allows the user to combine data types of different kinds into a single type in a block of memory. A structure can contain both simple and complex data types related to each other, which otherwise, won’t make sense. Each member within a structure gets its own memory location so that it can be accessed and retrieved at any time.

A structure is used when a lot of data needs grouping like a directory which stores information about many members in a book or an address book that stores all the information about a single contact – name, address, contact number, and so on. The address of each member will be in ascending order which means the memory of each member will start at different offset values. Altering the value of one member won’t have any effect on other members.

A ‘struct’ keyword is used to define a structure of different data types under a single name. For example, if you want to create an employee database, you’ll need to store employee’s name, age, phone, and salary details under a single entity. This is done by the keyword ‘struct’, which then tells the compiler that a structure has been declared.

struct employee{

string name;

string age;

string phone;

string salary;

} emp1, emp2;

Here, ‘employee’ is the name of the structure and two variables ‘emp1’ and ‘emp2’ are created of the type ‘employee’. The closing braces must be followed by a semicolon (;).

Members of a structure can be accessed by two types of operators:

  1. Member operator
  2. Structure pointer operator

What is Union?

It’s a special data type used in C that allows the user to store data types of different kinds in the same memory location. A union is quite similar to a structure and it can be defined by replacing the keyword ‘struct’ with the keyword ‘union’. The members overlay the memory of each other in a union and it is large enough to fit all its members.

When a variable is associated with union, the compiler allocates the memory by considering the size of the largest memory, so the size of a union is equal to the size of the largest data member. A union can be defined with many members, but only one member contains the value at any given instance of time.

The address is same for all members of a union which means every member starts at the same offset value. And altering the value of one member will affect values of other members. A union can be used when you wish to store something that’s one of several data types.

union employee {

char name [32];

int age;

float salary;

};

Here the keyword ‘union’ defines the union which is quite similar to the declaration of the structure. The variable can either store a string value which is ‘name of the employee’, an integer value which is the ‘age of the employee’, or a float which represents the ‘salary of the employee’.

Difference Between Structure and Union

1. Keyword

The keyword ‘struct’ is used to define a structure whereas ‘union’ keyword is used to define a union.

2. Memory Allocation

Separate memory space is allotted for the members within a structure and members have different addresses that do not share memory. A union, on the other hand, shares the same memory space for all its members so shared memory location is allocated.

3. Member Access

A union stores a single value at a time for all its members making accessibility to only one member at a time. While multiple values can be stored in a structure so any member value can be accessed and retrieved at any time.

4. Size

The size of a structure is equal to the sum of the size of all members or more, whereas the size of a union is equal to the size of the largest size member.

5. Initialization

In a structure, several members can be initialized at once, while in a union, only the first member can be initialized with the value of its type.

5. Value

A structure can store different values of all the members and change in the value of one member will not have any effect on the values of other members. While a union stores same value for all its members and change of value of one member will affect the value of other.

Structure vs. Union

Structure Union
Struct keyword is used to define a structure. Union keyword is used to define a union.
Members do not share memory in a structure. Members share the memory space in a union.
Any member can be retrieved at any time in a structure. Only one member can be accessed at a time in a union.
Several members of a structure can be initialized at once. Only the first member can be initialized.
Size of the structure is equal to the sum of size of the each member. Size of the union is equal to the size of the largest member.
Altering value of one member will not affect the value of another. Change in value of one member will affect other member values.
Stores different values for all the members. Stores same value for all the members.

Summary

  • Both the structure and union are user-defined data types in C that are functionally and conceptually the same yet are quite different in some ways.
  •  Both contain variables of different data types but they use the same syntax for declaration of variables and accessing members. While a structure is defined by the ‘struct’ keyword, a union is defined by the keyword ‘union’.
  • Each member gets separate memory location in a structure, whereas in a union, the total memory space is equivalent to the largest size member. All the members share the same memory space in a union.
  • Any member in any sequence can be accessed in a structure, whereas in a union, only that variable can be accessed the value of which is recently stored.
Latest posts by Sagar Khillar (see all)

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Kanetkar, Yashavant. Let Us C. New Delhi: BPB Publications, 2002. Print

[1]Kanetkar, Yashavant. Let Us C. New Delhi: BPB Publications, 2002. Print

[2]King, K.N. C Programming: A Modern Approach. New York: W.W. Norton & Company, 1996. Print

[3]Griffiths, David and Dawn Griffiths. Head First C. Sebastopol: O’Reilly Media, 2011. Print

[4]Image Credit: https://stackoverflow.com/questions/3921931/how-to-visualise-a-graph-of-c-structs-that-contain-point-to-one-another

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder