Start of change

%OFFSET built-in function

The offset built-in function (%OFFSET or %OFS) can be used to store or change the offset portion of a CL pointer variable and can only be used within a CL procedure.

In a CHGVAR command, the %OFFSET function can be used two different ways:
  • You can specify the %OFFSET function for the variable (VAR parameter) to set the offset portion of a pointer variable.
  • You can specify the %OFFSET function for the value (VALUE parameter) to which the variable is to be changed.
In an IF command, the %OFFSET function can be specified in the expression (COND parameter) and is treated like a four-byte unsigned integer value.
The format of the offset built-in function is:
%OFFSET(variable name)
or
%OFS(variable name)

In the following example, pointer variable &P1 has no initial value specified and therefore is initialized to null. The first CHGVAR will store the offset from the null pointer value in integer variable &I1; the command will run without an error, and the value of &I1 will probably be zero, though the offset for a null pointer is not defined. The second CHGVAR command sets &P1 to the address of local character variable &C1; pointer &P1 now points to byte 1 of variable &C1. The third CHGVAR command will store the offset portion of &P1 into the integer variable &I2. Note that the offset is from the start of the storage for all automatic variables for the current thread and not from the start of automatic storage for the current program or from the start of variable &C1. The fourth CHGVAR command takes the integer value stored in &I2, adds one hundred, and stores the resulting number into the integer variable &I3. The fifth CHGVAR command changes the offset portion of pointer &P1 using integer variable &I3; pointer &P1 now points to byte 101 of variable &C1. The sixth CHGVAR command will calculate the integer expression for the VALUE parameter by storing the offset portion of pointer &P1 into a temporary integer variable and subtracting 20, and use this calculated value to reset the offset portion of pointer &P1; pointer &P1 now points to byte 81 of variable &C1.

PGM                                                                    
DCL  &C1  *CHAR 30000                                                             
DCL  &P1  *PTR                                                                     
DCL  &P2  *PTR                                                                     
DCL  &I1  *UINT 4                                                                   
DCL  &I2  *UINT 4                                                                  
DCL  &I3  *UINT 4                                                                  
CHGVAR &I1 %OFFSET(&P1)                /* 1 */
CHGVAR &P1 %ADDRESS(&C1)               /* 2 */
CHGVAR &I2 %OFFSET(&P1)                /* 3 */   
CHGVAR &I3 (%I2+100)                   /* 4 */  
CHGVAR %OFFSET(&P1) &I3                /* 5 */
CHGVAR %OFFSET(&P1) (%OFFSET(&P1)-20)  /* 6 */
ENDPGM
The %OFFSET built-in function cannot be used with a pointer variable that addresses teraspace storage.
Related tasks
Change the value of a variable
Related reference
Built-in functions for CL
Related information
CL command finder
Change Variable (CHGVAR) command
End of change