Skip to main content

Data Structure: STRUCT

A structure is a user-defined data type, which combines multiple variables of any data type into a logical unit. The variables declared within a structure are called members.

You make the type declaration of a structure in a DUT object which you create in theProjectAdd ObjectDUT menu or in the context menu of an application.

For more information, see: DUT

Syntax

TYPE <structure name>

STRUCT

    <member name> : <data type> := <initialization> ;

END_STRUCT

END_TYPE

<structure name>

This identifier is valid in the entire project so that you can use it like a standard data type.

<member name> : <data type> ;

Declaration of a member

Any number of declarations can follow, but at least 2.

Structures can also be nested. This means that you declare a structure member with an existing structure type. Then the only restriction is that you must not assign any address to the variable (structure member). The AT declaration is not permitted here.

:= <initialization>

Optional

Type declaration

TYPE S_POLYGONLINE :
STRUCT
    aiStart : ARRAY[1..2] OF INT := [-99, -99];
    aiPoint1 : ARRAY[1..2] OF INT;
    aiPoint2 : ARRAY[1..2] OF INT;
    aiPoint3 : ARRAY[1..2] OF INT;
    aiPoint4 : ARRAY[1..2] OF INT;
    aiEnd : ARRAY[1..2] OF INT := [99, 99];
END_STRUCT
END_TYPE

Extension of a type declaration

An additional structure is declared from an existing structure. In addition to its own members, the extended structure also has the same structure members as the base structure.

Syntax

TYPE <structure name> EXTENDS <base structure>

STRUCT

    <member declaration>

END_STRUCT

END_TYPE

Example 223. Example

Type declaration: S_PENTAGON

TYPE S_PENTAGON EXTENDS S_POLYGONLINE :
STRUCT
    aiPoint5 : ARRAY[1..2] OF INT;
END_STRUCT
END_TYPE


Declaration and initialization of structure variables

Example 224. Example
PROGRAM progLine
VAR
    sPolygon : S_POLYGONLINE := (aiStart:=[1,1], aiPoint1:=[5,2], aiPoint2:=[7,3], aiPoint3:=[8,5], aiPoint4:=[5,7], aiEnd:=[1,1]);
    sPentagon : S_PENTAGON := (aiStart:=[0,0], aiPoint1:=[1,1], aiPoint2:=[2,2], aiPoint3:=[3,3], aiPoint4:=[4,4], aiPoint5:=[5,5], aiEnd:=[0,0]);
END_VAR


You must not use initializations with variables. For an example of initializing an array of a structure, see the help page for the data type ARRAY OF.

Access to a structure member

You access structure members with the following syntax:

<variable name> . <member name>

Example 225. Example
PROGRAM prog_Polygon
VAR
    sPolygon : S_POLYGONLINE := (aiStart:=[1,1], aiPoint1:=[5,2], aiPoint2:=[7,3], aiPoint3:=[8,5], aiPoint4:=[5,7], aiEnd:=[1,1]);
    iPoint : INT;
END_VAR
// Assigs 5 to aiPoint
iPoint := sPolygon.aiPoint1[1];

Result: iPoint = 5



Symbolic bit access in structure variables

You can declare a structure with variables of data type BIT to combine individual bits into a logical unit. Then you can symbolically address individual bits by a name (instead of by a bit index).

Syntax declaration

TYPE <structure name> :

STRUCT

    <member name> : BIT;

END_STRUCT

END_TYPE

Syntax of bit access

<structure name> . <member name>

Example 226. Example

Type declaration

TYPE S_CONTROL :
STRUCT
    bitOperationEnabled : BIT;
    bitSwitchOnActive : BIT;
    bitEnableOperation : BIT;
    bitError : BIT;
    bitVoltageEnabled : BIT;
    bitQuickStop : BIT;
    bitSwitchOnLocked : BIT;
    bitWarning : BIT;
END_STRUCT
END_TYPE

Bit access

FUNCTION_BLOCK FB_Controller
VAR_INPUT
    xStart : BOOL;
END_VAR
VAR_OUTPUT
END_VAR
VAR
    ControlDriveA : S_CONTROL;
END_VAR

IF xStart = TRUE THEN
    // Symbolic bit access
    ControlDriveA.bitEnableOperation := TRUE;
END_IF

PROGRAM PLC_PRG
    fbController : FB_Controller;
END_VAR
   fbController();
fbController.xStart := TRUE;


For more information, see: Bit Access in Variables

Tip

References and pointers to BIT variables are invalid declarations, as well as array elements with base type BIT.