Attention

IronPLC implements many parts of the IEC 61131-3 standard and is working toward full Structured Text support. Key features still missing include arrays and structures. Try it out in the IronPLC Playground.

Reference Types

A reference type holds a pointer to a variable. References allow indirect access — reading or writing through the reference affects the original variable.

Note

This feature requires IEC 61131-3 Edition 3. See Enabling Features for how to enable it.

IEC 61131-3

Section 2.3.3.1 (Edition 3)

Support

Supported (Edition 3)

Syntax

Declare a reference variable with REF_TO:

variable_name : REF_TO element_type

You can also create a named reference type:

TYPE
    IntRef : REF_TO INT;
END_TYPE

Operators

REF()

Creates a reference to a variable:

r := REF(counter);
^ (dereference)

Reads or writes the referenced variable:

value := r^;    (* read through reference *)
r^ := 99;       (* write through reference *)
NULL

A literal representing an empty reference. Can be assigned to any REF_TO variable and compared with = or <>:

r := NULL;
IF r <> NULL THEN
    value := r^;
END_IF;

Example

Restrictions

  • REF() accepts only simple named variables (not array elements or literals).

  • References to temporary variables (VAR_TEMP, function parameters) are not allowed.

  • Nested references (REF_TO REF_TO) are not supported.

  • Arithmetic on references is not supported.

  • Only = and <> comparison operators work with references.

See Also