Sharing Memory with union type - C Data Type

C examples for Data Type:union

Introduction

union type allows sharing memory by a number of different variables.

The following statement declares a union to be shared by three variables:

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

Here's an example that makes use of a union.

Demo Code

#include <stdio.h>

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;/*from  w ww. ja  v  a 2 s  .com*/
  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, 2012};
  MixedDate today = {"Monday", "12th November", 2012};
  char tomorrow[] = "Tues 13th Nov 2012";

  // 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("The date is %d/%d/%d.\n", date->date.nDate.day,date->date.nDate.month,date->date.nDate.year);
      break;
    case text:
     printf("The date is %s.\n", date->date.date_str);
     break;
    case mixed:
     printf("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("Invalid date format.\n");
  }
}

Result


Related Tutorials