KEMBAR78
U4 4 Union | PDF | Pointer (Computer Programming) | Computer Science
0% found this document useful (0 votes)
31 views11 pages

U4 4 Union

The document discusses topics related to structures, unions, and enumerations in C programming. It provides information on the features of structures, declaring and initializing structures, arrays of structures, pointers to structures, and structures and functions. It then discusses unions, noting they are similar to structures but all members share the same memory location. The document compares structures and unions, and provides details on pointers to unions. It explains the need for unions in C programming and memory management.

Uploaded by

mit23cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views11 pages

U4 4 Union

The document discusses topics related to structures, unions, and enumerations in C programming. It provides information on the features of structures, declaring and initializing structures, arrays of structures, pointers to structures, and structures and functions. It then discusses unions, noting they are similar to structures but all members share the same memory location. The document compares structures and unions, and provides details on pointers to unions. It explains the need for unions in C programming and memory management.

Uploaded by

mit23cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT IV

Topics to be Covered:
Storage class
Structure
 Features of structures
 Declaration and initialization of structures
 Array of structures
 Pointer to structure
 Structure and functions
Typedef
bit fields
enumerated data types

Union
12/18/2023 BVL_Computer Centre, MIT Campus 1
Unions
• Unions are conceptually similar to structures in C.
• The syntax to declare/define a union is also similar to
that of a structure instead of struct keyword union
keyword need to be used.
• The only differences is in terms of storage.
• In structure each member has its own storage location,
whereas all members of union uses a single shared
memory location which is equal to the size of its largest
data member.

12/18/2023 BVL_Computer Centre, MIT Campus 2


Difference between structure and union

Output : Output :

12/18/2023 BVL_Computer Centre, MIT Campus 3


Unions – Cont’d
• Unions are commonly used when there is a need to
store different types of data in a specific memory
location.
• They are especially useful in scenarios where we need
to save memory or when different types of data need
to be accessed interchangeably.
• However, it’s important to note that only one
member of a union can be accessed at a time.
• The value of one member can overwrite the value of
another member, leading to unpredictable behavior if
the union is not used carefully.

12/18/2023 BVL_Computer Centre, MIT Campus 4


Output :

12/18/2023 BVL_Computer Centre, MIT Campus 5


Need for Union in C programming

• C unions are used to save memory.


• C unions allow data members which are mutually exclusive to share
the same memory.
• This is quite important when memory is valuable, such as in
embedded systems.
• Unions are mostly used in embedded programming where direct
access to the memory is needed.

12/18/2023 BVL_Computer Centre, MIT Campus 6


Output :

12/18/2023 BVL_Computer Centre, MIT Campus 7


Pointer to Union
• Pointer to union can be created just like other pointers to
primitive data types.
• Consider the below union declaration:
union number
{
int a;
int b;
};
• Here, a and b are the members of the union number.

12/18/2023 BVL_Computer Centre, MIT Campus 8


Pointer to Union – Cont’d
• Union variable declaration:
union number n;
• Here, n is the variable to number.
• Pointer to union declaration and initialization:
union number *ptr = &n;
• Here, ptr is the pointer to the union and it is assigning with the
address of the union variable n.
• Accessing union member using union to pointer:
• Syntax:
pointer_name->member;
Example:
// to access members a and b using ptr
ptr->a;
ptr->b;

12/18/2023 BVL_Computer Centre, MIT Campus 9


Output :

12/18/2023 BVL_Computer Centre, MIT Campus 10


Difference between Union and Structure
Union Structure
Each member has its own separate All members share the same memory
memory space. space.
The size of the structure is determined
The size of the union is determined by
by the sum of the sizes of its
the size of its largest member.
members.

Only one member can be accessed at Individual members can be accessed


a time. at a time using the dot operator.

Only the first member can be All members can be initialized


initialized explicitly. individually or collectively.
Example: Example:
union student struct student
{ {
int id; int id;
char name[12]; char name[12];
12/18/2023 BVL_Computer Centre, MIT Campus 11
}; };

You might also like