Pointer

Pointers store the addresses of variables, programs, function blocks, methods and functions while an application program is running. A pointer points to one of the objects mentioned or to a variable with any data type.

Syntax of a pointer declaration:

<identifier>: POINTER TO <data type | function block | program | method | function>;

Dereferencing a pointer means obtaining the value of the address to which the pointer points. You can dereference a pointer by appending the content operator to the pointer identifier, see for example pt^ in the example shown below.

You can use the address operator ADR in order to assign the address of a variable to a pointer.

Example

VAR
 pt:POINTER TO INT;  (* declaration of pointer pt *)
 var_int1:INT := 5;  (* declaration of variables var_int1 and var_int2 *)
 var_int2:INT;
 END_VAR

pt := ADR(var_int1); (* pointer pt is assigned to address of varint1 *)
var_int2:= pt^;      (* value 5 of var_int1 is assigned to variable var_int2 by dereferencing of pointer pt *)

Hint

If a pointer to a device input is used, the access (for example pTest := ADR(input);) is considered to be a write access. When generating code this leads to a compiler warning "….no valid assignment target".

If you require a construct of this kind, you must first copy the input value (input) to a variable with write access.

See also

Function pointers to external functions

CODESYS supports function pointers that replace the INDEXOF operator. You can pass these pointers on to external libraries. However, CODESYS offers no possibility to call a function pointer within an application in the programming system! The function of the runtime system for the registration of callback functions (system library function) expects the function pointer. Depending on which callback was registered, the runtime system implicitly calls the function concerned (for example in case of STOP). So that such a system call is possible (runtime system), you must set the corresponding object property in the Build tab.

You can use the ADR operator for functions, programs, function blocks and methods. Since functions can change their value after an online change, CODESYS does not output the address of the function, but the address of a pointer to the function. This address is valid as long as the function exists on the target system.

See also

Index access to pointers

In CODESYS the index access ‘[]’ is permissible to variables of the type POINTER, STRING or WSTRING.

Note

The result of the difference between two pointers is of the type DWORD, even on 64-bit platforms, if the pointers are 64-bit pointers.

Note

Note the possibility to use references which, in contrast to pointers, directly control a value.

Note

Note the possibility to monitor the memory access of pointers at runtime by the implicit monitoring function CheckPointer

See also