Access union value in C language
A union is custom or user define data type which can have multiple labels with different data type. The value of the union can be access using label name and either by using dot (.) operator or arrow (->) operator. The arrow operator used when union variable define as pointer variable.

Access members using dot operator

A memory location of union can be access via dot operator by using labels, if variable is non pointer variable. The dot operator allows to read and write the value into memory location by specifying labels. It allows to read and write value using scanf function as well.
Access union member using dot operator
 1 #include <stdio.h>
 2 union Union {  
 3     int num;  
 4     char ch;   
 5 };
 6 
 7 int main() {
 8     // creating local variable of union
 9     union Union var;
 10     
 11     var.num = 10;
 12     printf("Value of A is : %d", var.num);
 13     
 14     printf("\nEnter character to set : ");
 15     // all union members share same memory, hence below line
 16     // override the previous value (ie. 10)
 17     scanf("%c", &var.ch);
 18     
 19     printf("\nValue of union num is : %d", var.num);
 20     printf("\nValue of union is : %c", var.ch);
 21     
 22     return 0;
 23 }
In the above example, union is define with two labels with num and ch. In the main method, we have define variable of union and setting values. Initially value of num is set to 10. By using scanf function, we are setting new value to variable ch. As union members share same memory location. Hence, it overrides the previous value of num (10).
Output
 1 Value of A is : 10
 2 Enter character to set : A
 3 Value of union num is : 65 // ASCII value of 'A' is 65
 4 Value of union is : A

Access members using arrow operator

A members of union access via arrow operator, if variable is pointer variable. The arrow operator allows to read and write the memory location by using labels. The union memory location can be read and set using scanf function as well.
Access members using arrow operator
 1 #include <stdio.h>
 2 union Union {  
 3     int num;  
 4     char ch;   
 5 }var;
 6 
 7 int main() {
 8     // creating pointer variable
 9     union Union *ptr;
 10     ptr = &var;
 11     ptr->num = 10;
 12     printf("Value of A is : %d", ptr->num);
 13     
 14     printf("\nEnter character to set : ");
 15     // all union members share same memory, hence below line
 16     // override the previous value (ie. 10)
 17     scanf("%c", &(ptr->ch));
 18     
 19     printf("\nValue of union num is : %d", ptr->num);
 20     printf("\nValue of union is : %c", ptr->ch);
 21     
 22     return 0;
 23 }
In the above example, union is define with two labels with num and ch. In the main method, we have define variable of union and setting values. Initially value of num is set to 10. By using scanf function, we are setting new value to variable ch. As union members share same memory location. Hence, it overrides the previous value of num (10).
Output
 1 Value of A is : 10
 2 Enter character to set : A
 3 Value of union num is : 65 // ASCII value of 'A' is 65
 4 Value of union is : A
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us