Union

A union is like a structure in which all members are stored at the sameaddress. Members of a union can only be accessed one at a time. The union data type was invented to prevent memory fragmentation. The union data type prevents fragmentation by creating a standard size for certain data. Just like with structures, the members of unions can be accessed with the . and -> operators. Take a look at the example:

 #include<stdio.h>

 typedef union myunion
 {
  double PI;
  int B;
 }MYUNION;

 int main()
 {
  MYUNION numbers;
  numbers.PI = 3.14;
  numbers.B = 50;

 return 0;
 }



Spread the word! By sharing this Post!

Pageviews This Week