SA0007: Address operator on constants
Detects lines of code where the operator ADR
is applied for a constant
Justification: Using a pointer to a constant variables overrides the CONSTANT
property of the variable. The variable can be changed by means of the pointer without any notification from the compiler.
Exception: In rare cases, it might be useful to pass a pointer to a constant to a function. However, you need to make sure that this function does not change the transferred value. Whenever possible, use VAR_IN_OUT CONSTANT
.
Importance: High
Tip
When the Replace constants option is selected in the Compiler Options of the project settings, the address operator is not permitted for scalar constants (integer, BOOL
, REAL
) and a compile error is issued. (Constant strings, structures, and arrays always have an address.)
PROGRAM PLC_PRG VAR CONSTANT c_iValue : INT := INT#15; END_VAR VAR poiValue : POINTER TO INT; END_VAR
poiValue := ADR(c_iValue); // SA0007
Output in the Messages view:
SA0007: Address to constant variable 'c_iValue'