IEC 61131-3 Pointer Type

Introduction

A pointer type declares a variable that takes the address of another variable. For example:

Type
    pVoltage : POINTER TO INT;
End_Type

You can assign the address of a variable using the address operator, commercial at (@) character. For example:

Var
    V1, V2 : INT;
    pV : Pointer to INT;
End_Var

    (* Get the address of V2 *)
    pV := @V2;

The dereference operator, circumflex accent (^) character, is used to dereference the pointer. For example:

(* Take the address of V2 *)
pV := @V2;

(* Assign the value 12 to V2 via the pointer pV *)
pV^ := 12;

(* Assign the value of V2 to V1 via the pointer pV *)
V1 := pV^;

The NULL keyword, can be used to indicate no address. If a code statement attempts to dereference a pointer with no address, the program will halt with the error "Null pointer dereference". As a defensive coding technique you can test for NULL using an IF statement:

IF pV <> NULL THEN
    pV^ := 12;
END_IF;

Constant Pointers

Pointers can be declared constant, which prevents the target of the pointer being written to. For example:

Type
    pVoltage : POINTER TO CONSTANT INT;
End_Type

The compiler will report an error if you try and write a value via a constant pointer. For example:

Type
    V : INT;
    pVoltage : POINTER TO CONSTANT INT;
End_Type

    pV := @V;

    (* Compiler will report an error for this line... *)
    pV^ := 12;

Standards Compliance

The POINTER TO type modifier is an extension to the IEC 61131-3 standard.

Further Information

Derived Data Types

To learn about other derived data types, for example STRUCT.

Elementary Types

To learn about the built-in elementary types for example INT, or TIME.

Common Elements

To learn about other common language elements.

Character Codes

For a list of all (U+nnnn) character codes.

Glossary

For definitions of the terms used in Fernhill SCADA.