Holla Tech - Learn

Unions
 

A union allows to store different data types in the same memory location.
It is like a structure because it has members. However, a union variable uses the same memory location for all its member’s and only one member at a time can occupy the memory location.

A union declaration uses the keyword union, a union tag, and curly braces { } with a list of members.

Union members can be of any data type, including basic types, strings, arrays, pointers, and structures.
For example: 

union val {
  int int_num;
  float fl_num;
  char str[20];
}; 

After declaring a union, you can declare union variables. You can even assign one union to another of the same type:

union val u1;
union val u2;
u2 = u1

 

Unions are used for memory management. The largest member data type is used to determine the size of the memory to share and then all members use this one location. 

NOTE!
This process also helps limit memory fragmentation. Memory management is discussed in a later lesson.

Accessing Union Members
 

You access the members of a union variable by using the . dot operator between the variable name and the member name.
When assignment is performed, the union memory location will be used for that member until another member assignment is performed.

Trying to access a member that isn’t occupying the memory location gives unexpected results.

The following program demonstrates accessing union members:

union val {
  int int_num;
  float fl_num;
  char str[20];
};
 
union val test;

test.int_num = 123;
test.fl_num = 98.76;
strcpy(test.str, “hello”);

printf(“%d\n”, test.int_num);
printf(“%f\n”, test.fl_num);
printf(“%s\n”, test.str); 

 

The last assignment overrides previous assignments, which is why str stores a value and accessing int_num and fl_num is meaningless.


Structures With Unions
 

Unions are often used within structures because a structure can have a member to keep track of which union member stores a value.
For example, in the following program, a vehicle struct uses either a vehicle identification number (VIN) or an assigned id, but not both:

typedef struct {
  char make[20];
  int model_year;
  int id_type; /* 0 for id_num, 1 for VIN */
  union {
    int id_num;
    char VIN[20];
  } id;
} vehicle;

vehicle car1;
strcpy(car1.make, “Ford”);
car1.model_year = 2017;
car1.id_type = 0;
car1.id.id_num = 123098

 

Note that the union was declared inside the structure. When doing this, a union name was required at the end of the declaration.
A union with a union tag could have been declared outside the structure, but with such a specific use, the union within the struct provides easier to understand the code.

Note also the dot operator is used twice to access union members of struct members.

The id_type keeps track of which union member stores a value. The following statements display car1 data, using the id_type to determine which union member to read:

/* display vehicle data */
printf(“Make: %s\n”, car1.make);
printf(“Model Year: %d\n”, car1.model_year);
if (car1.id_type == 0)
  printf(“ID: %d\n”, car1.id.id_num);
else
  printf(“ID: %s\n”, car1.id.VIN);

 

NOTE!
A union can also contain a structure.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE C MAIN COURSE PAGE. 

C MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT