PLC

UNION

A UNION is a data structure that usually contains different data types. In a union all components have the same offset, which means they occupy the same memory space.

You declare an union in a DUT object that you create using the command Add > DUT in the context menu of the PLC project tree in the project.

Syntax:

TYPE <union name>:
UNION
    <Variable declaration 1>
    ...
    <Variable declaration n>
END_UNION
END_TYPE

Example:

In the following sample declaration of a union, an assignment to uName.fA would also affect uName.nB and uName.nC.

TYPE U_Name:
UNION
    fA : LREAL;
    nB : LINT;
    nC : WORD;
END_UNION
END_TYPE

Declaration:

uName : U_Name;

Program:

uName.fA := 1;

Result:

fA = 1
nB = 16#3FF0000000000000
nC = 0

See also:

Object DUT