Start of change

Use based variables

Based variables can be used to map variables passed to the program or manipulate arrays of values.

The basing pointer must be set using the ADDRESS keyword on the Declare (DCL Command) or with the %ADDRESS built-in function before being used. After the basing pointer is set, the variables will work like local variables.

In the following example, the basing pointer &PTR is declared to be equal to the address of &AUTO. The variable &BASED then has the value of the first 10 bytes addressed by the pointer variable &PTR. Later in the procedure, the value of variable &BASED is checked for equality against the first 10 bytes of variable &AUTO. If the values are the same, meaning pointer &PTR addresses the first byte of &AUTO, the pointer offset is changed to address byte 11 of variable &AUTO. Now variable &BASED has a value equal to bytes 11-20 of variable &AUTO.

PGM
DCL &AUTO  *CHAR 20
DCL &PTR   *PTR ADDRESS(&AUTO)
DCL &BASED *CHAR 10 STG(*BASED) BASPTR(&PTR) 
:
IF COND(%SST(&AUTO 1 10) *EQ &BASED) +
   THEN(CHGVAR %OFS(&PTR) (%OFS(&PTR) + 10))
:
ENDPGM
Related information
Declare CL Variable (DCL) command
End of change