Learn C - C Union






Sharing Memory

C can share same memory area by a number of different variables via a union.

A union is usually given a tag name in the same way as structure.

You use the keyword union to define a union.

For example, the following statement declares a union to be shared by three variables:

  
union U_example 
{ 
  float decval; 
  int *pnum; 
  double my_value; 
} u1; 

This statement declares a union with the tag name U_example, which shares memory between decval, pnum, and my_value.

The statement also defines one instance of the union with a variable name of u1.

You can declare further instances of this union with a statement such as this:

union U_example u2, u3;

Members of a union are accessed in exactly the same way as members of a structure.

For example, to assign values to members of u1 and u2, you can write this:

u1.decval = 2.5; 
u2.decval = 3.5*u1.decval; 

The size of an instance of a union is the memory required for the largest member.





Example


#include <stdio.h> 
      // ww w . ja va2s .  com
typedef union UDate UDate; 
typedef struct Date Date; 
typedef struct MixedDate MixedDate; 
typedef struct NumericDate NumericDate; 

void print_date(const Date* date);            // Prototype 

enum Date_Format{numeric, text, mixed};       // Date formats 

struct MixedDate { 
  char *day; 
  char *date; 
  int year; 
}; 
struct NumericDate { 
  int day; 
  int month; 
  int year; 
}; 
union UDate { 
  char *date_str; 
  MixedDate day_date; 
  NumericDate nDate; 
}; 
struct Date { 
  enum Date_Format format; 
  UDate date; 
}; 

int main(void) { 
  NumericDate yesterday = { 11, 11, 2020}; 
  MixedDate today = {"Monday", "12th November", 2020}; 
  char tomorrow[] = "Tues 13th Nov 2020"; 

  // Create Date object with a numeric date 
  UDate udate = {tomorrow}; 
  Date the_date; 
  the_date.date = udate; 
  the_date.format = text; 
  print_date(&the_date); 

  // Create Date object with a text date 
  the_date.date.nDate = yesterday; 
  the_date.format = numeric; 
  print_date(&the_date); 
      
  // Create Date object with a mixed date 
  the_date.date.day_date = today; 
  the_date.format = mixed; 
  print_date(&the_date); 
  
  return 0; 
} 

void print_date(const Date* date) { 
      switch(date->format) { 
        case numeric: 
          printf_s("The date is %d/%d/%d.\n", date->date.nDate.day, 
                                              date->date.nDate.month, 
                                              date->date.nDate.year); 
          break; 
        case text: 
         printf_s("The date is %s.\n", date->date.date_str); 
         break; 
        case mixed: 
         printf_s("The date is %s %s %d.\n", date->date.day_date.day, 
                                             date->date.day_date.date, 
                                             date->date.day_date.year); 
          break; 
        default: 
          printf_s("Invalid date format.\n"); 
      } 
} 

The code above generates the following result.