SA0007: Address operator on constantsPermalink

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 have to make sure that this function does not change the transferred value. Whenever possible, use ‎VAR_IN_OUT CONSTANT‎.

Importance: High

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 complie error is issued. (Constant strings, structures, and arrays always have an address.)

Example

PROGRAM PLC_PRG
VAR CONSTANT
	c_iValue : INT := INT#15;
END_VAR
VAR
	poiValue : POINTER TO INT;
END_VAR
poiValue := ADR(c_iValue); // SA0007

--> SA0007: Address to constant variable 'c_iValue'