how to create a Pointer in ST (TC3)

#PLC #TwinCAT3 #Beckhoff

Learn more at Part 4 - Data types & arrays (IEC 61131-3)

1) Example

PROGRAM MAIN
VAR
	// Create a Variable
	nVariable : INT := 20;
	// Create a Pointer and get the address of the Variable
	pPointer : POINTER TO INT := ADR(nVariable);		
END_VAR

//Dereference and change the content of Variable
pPointer^ := 30;

2) Documentation

Pointers are symbolic representation of addresses. They are a symbolic representation for an address in the memory of the PLC so that you can access a location of the computers memory by a name.

The pointer is a variable in itself, it holds the address of the memory location of the variable it is pointing to. Thus in a 64-bit system, a pointer uses 64 bits of memory, in other words 8 bytes.

What we have in this example is a pointer to an integer. If we write it like this, this pointer doesn't point to anything. If we would look at the value of the pointer now, it would just be the address zero.

REMEMBER: the pointer doesn't hold an integer but an ADDRESS to a memory location

If we want to change the value of the variable that the pointer is pointing to, you have dereference it, by applying the content operator to the pointer identifier.

Now we have updated the actual value of the memory location from the initial 20 to 30