A union declaration defines groups of fields that share memory at runtime.
The syntax of a union declaration is:
UNION map-declaration map-declaration [map-declaration ] ... [map-declaration ] END UNION
The syntax of a map declaration is as follows.
MAP field-declaration [field-declaration] ... [field-declaration] END MAP
Each field-declaration in a map declaration can be one of the following:
Structure declaration
Record
Union declaration
Declaration of a typed data field
A map declaration defines alternate groups of fields in a union. During execution, one map at a time is associated with a shared storage location. When you reference a field in a map, the fields in any previous map become undefined and are succeeded by the fields in the map of the newly referenced field. The amount of memory used by a union is that of its biggest map.
Example: Declare the structure /STUDENT/ to contain either NAME, CLASS, and MAJOR--or NAME, CLASS, CREDITS, and GRAD_DATE:
STRUCTURE /STUDENT/ CHARACTER*32 NAME INTEGER*2 CLASS UNION MAP CHARACTER*16 MAJOR END MAP MAP INTEGER*2 CREDITS CHARACTER*8 GRAD_DATE END MAP END UNION END STRUCTURE
If you define the variable PERSON to have the structure /STUDENT/ from the above example, then PERSON.MAJOR references a field from the first map, and PERSON.CREDITS references a field from the second map. If the variables of the second map field are initialized, and then the program references the variable PERSON.MAJOR, the first map becomes active, and the variables of the second map become undefined.