Accessing Members of Union : C Programming

Accessing Members of Union in C Programming

While accessing union, we can have access to single data member at a time. we can access single union member using following two Operators -

  • Using DOT Operator
  • Using ARROW Operator

Accessing Union using DOT Operator :

We can access structure member using the dot operator. DOT operator is used inside printf and scanf statement to get/set value from/of union member location.

Syntax :

Variable_name.Member
OR
Pointer_Variable_name->Member

Dot Operator

union emp
{
int id;
char name[20];
}e1;

id can be Accessed by - [union_Variable.member]

e1.id

Accessing Member of Union using Arrow Operator :

Instead of maintaing the union variable suppose we store union at particular address then we can access the members of the union using pointer to the union and arrow operator.

union emp
{
int id;
char name[20];
}*e1;

id can be Accessed by - [union_Variable->member]

e1->id