Using an anonymous union. - C++ Data Type

C++ examples for Data Type:union

Description

Using an anonymous union.

Demo Code

#include <iostream> 
using namespace std; 

int main() /*from  ww  w .jav  a 2 s . co  m*/
{ 
   // declare an anonymous union 
   union 
   { 
       int integer1; 
       double double1; 
       char *charPtr; 
   };

   int integer2 = 1; 
   double double2 = 3.3; 
   char *char2Ptr = "Anonymous"; 

   cout << integer2 << ' '; 
   integer1 = 2; 
   cout << integer1 << endl; 

   cout << double2 << ' '; 
   double1 = 4.4; 
   cout << double1 << endl; 

   cout << char2Ptr << ' '; 
   charPtr = "union"; 
   cout << charPtr << endl; 
}

Result


Related Tutorials