%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 program or 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 command stores the offset from the null pointer value in integer variable &OFF1; the command runs without an error, and the value of &OFF1 is probably 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 stores the offset portion of &P1 into the integer variable &OFF2. 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 &OFF2, adds one hundred, and stores the resulting number into the integer variable &OFF3. The fifth CHGVAR command changes the offset portion of pointer &P1 using integer variable &OFF3; pointer &P1 now points to byte 101 of variable &C1. The sixth CHGVAR command calculates the integer expression for the VALUE parameter by storing the offset portion of pointer &P1 into a temporary integer variable and subtracting 20, and then uses this calculated value to reset the offset portion of pointer &P1; pointer &P1 now points to byte 81 of variable &C1.

Note: By using the code example, you agree to the terms of the Code license and disclaimer information.
PGM                                                                                
DCL  &C1  *CHAR 30000                                                              
DCL  &P1  *PTR                                                                     
DCL  &P2  *PTR                                                                     
DCL  &OFF1  *UINT 4                                                                
DCL  &OFF2  *UINT 4                                                                
DCL  &OFF3  *UINT 4                                                                
CHGVAR &OFF1 %OFFSET(&P1)                /* 1 */
CHGVAR &P1 %ADDRESS(&C1)                 /* 2 */
CHGVAR &OFF2 %OFFSET(&P1)                /* 3 */
CHGVAR &OFF3 (&OFF2+100)                 /* 4 */
CHGVAR %OFFSET(&P1) &OFF3                /* 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.