how to use SIZEOF() operator in ST (TC3)
Learn more at Part 4 - Data types and arrays (IEC 61131-3)
1) Example
PROGRAM MAIN
VAR
fOneFloat : REAL;
sThisIsAString : STRING;
nAnInteger : INT;
nReturnedNumberOfBytesForFloat : UDINT;
nReturnedNumberOfBytesForString : UDINT;
nReturnedNumberOfBytesForInteger : UDINT;
END_VAR
------------------------------------------------------------------------
nReturnedNumberOfBytesForFloat := SIZEOF(fOneFloat);
nReturnedNumberOfBytesForString := SIZEOF(sThisIsAString);
nReturnedNumberOfBytesForInteger := SIZEOF(nAnInteger);
2) Documentation
This operator is used for is to determine the required size in bytes for a specific variable.
Note: the SIZEOF()
operator is very useful when you work with buffers
You can learn more here
3) Pointers and References: using the SIZEOF() operator
You can do a SIZEOF() on the reference, and get the size of the referenced object.
So here we have an integer, that is 2 bytes, and the SIZEOF() the reference is 2.
If you would do a SIZEOF() on a pointer, you would get the size of the pointer, not the SIZEOF the object the pointer is pointing to.
So in this example we have an integer, and a pointer to an integer. And the size isn't the size of the integer, but the size of the pointer. So again if we have a 64-bit system then the size of the pointer is 8 bytes.