Pointer data type
A pointer data type is an extension of EN 61131 (it is not defined there). The reason is safety of programming: Wrong usage of pointer may result in collapsing of the program, which is unacceptable for technology control. This type of error can not be detected neither at the compilation, nor at the runtime. On the other hand, pointers bring benefits like higher efficiency programming, and solving tasks which could not be solved by any other means.
A pointer “points” at a variable which may be of elementary or derived type. In other words, a pointer contains the address of a variable. There are two types of operations which can be done with a pointer: Either its value can be changed (decreased / increased) which changes the variable the pointer is pointing at, or the value of the pointed variable can be processed. The former operation is called pointer arithmetics, tha latter is pointer dereference.
- A pointer is declared by using the keyword PTR_TO. Then, the data type name follows which the pointer is pointing at.
- A pointer is initialised by using the ADR() function. Parameter of this function is he name of the variable which address we want to fill in the pointer.
- Pointer dereference is an operation which makes it possible to work with the variable the pointer is pointing at. To dereference, the character ^ is used. The types in pointer declaration and variable declaration must be the same.
Example
VAR MyPtr : PTR_TO INT; MyVar : INT; END_VAR MyVar := 100; MyPtr := ADR(MyVar); //setting of the pointer to a variable MyPtr^ := 200; //variable value has been set to 200