



NOTICE

With compiler version >= V3.3.0.0, references are initialized (at 0).




NOTICE

If a reference refers to a device input, then the access is applied as write access.
When the code is generated, this leads to the compiler warning: "...invalid assignment target".
Example: rInput REF= Input;
If you require a construct of this kind, you have to first copy the input value (example:
rInput) to a variable with write access.
A reference with the REFERENCE data type implicitly refers to another object. The assignment is done with the ⮫ REF= operator. When accessed, the reference is implicitly dereferenced, and therefore does not
need a special content operator ^ such as a pointer.
Syntax
<identifier> : REFERENCE TO <data type> ; <data type>: base type of the reference
Valid declaration
PROGRAM PLC_PRG
VAR
rspeA : REFERENCE TO DUT_SPECIAL;
pspeA : POINTER TO DUT_SPECIAL;
speB : DUT_SPECIAL;
END_VAR
rspeA REF= speB; // Reference rspeA is alias for speB. The code corresponds to pspeA := ADR(speB);
rspeA := speD; // The code corresponds to pspeA^ := speD;
Invalid declarations
ariTest : ARRAY[0..9] OF REFERENCE TO INT; priTest : POINTER TO REFERENCE TO INT; rriTest : REFERENCE TO REFERENCE TO INT; rbitTest : REFERENCE TO BIT;
A reference type must not be used as the base type of an array, pointer, or reference. Furthermore, a reference must not refer to a bit variable. These kinds of constructs generate compiler errors.
The readability of a program is made difficult when the same memory cell is accessed simultaneously by means of an identifier and its alias.
Example: speB and rspeA
References and pointers to BIT variables are invalid declarations, as well as array elements with base type BIT.
Examples
When references are assigned with :=, a value is always copied, no matter whether the reference is on the left, on the right, or on both sides:
-
Ref := value writes the value value to the location where the reference points to. In pointer notation: Ref^ := value
-
value := Ref writes the value which the reference points to after value. In pointer notation: value := Ref^
-
Ref1 := Ref2 writes the value which Ref2 points to. at the location where Ref1 points to. In pointer notation: Ref1^ := Ref2^
When REF= is used, the address is always applied and there has to be a reference on the left side:
-
Ref REF= value: The reference points to value. In pointer notation: Ref :=ADR(value)
-
Ref1 REF= Ref2: Ref1 points to the same value as Ref2. In pointer notation: Ref1 := Ref2
-
Value REF = Ref leads to a compile error
In the declaration, REFERENCE TO INT REF= value behaves like REFERENCE TO INT := value.
-
Comparison of reference and pointer
-
Testing the validity of a reference