I am trying to learn the use of pointers in PL1. However below approach is not working
DCL MYPTR PTR;
DCL MYVAR CHAR(10) INIT('1234567890');
PUT SKIP(2) LIST('DISPLAY MYVAR: ',MYVAR);
MYPTR = ADDR(MYVAR);
PUT SKIP(2) LIST('DISPLAY MYPTR: ',MYPTR);
OUTPUT:
DISPLAY MYVAR: 1234567890
DISPLAY MYPTR:
Pointers are variables that contain a storage address. You use them to remap a storage area with different layout. For example, assume you've got records in a single data set that have different layout, say:
These declarations set aside two distinct storage areas, one for each type. Note that the records have different lengths. How would you read in the records, if you only know the type, and with this the length after having read the record? You would need to do something like:
This involves a lot of unnecessary data moves.
Using pointers, and the associated
based()attribute in declarations, you can define the structures as mapping, i.e. witout underlying storage. You then use a single pointer for all mappings.Now, you do something like this:
LongestRecordfield,RecType(assuming the type indicator is at the same position for each type).RecordType1.Field01, orRecordType2.Number02No more unnecessary data moves from input area to mapping area..
If you read the records from a data set, you can even avoid the first move and access the records directly in the input buffer; just tell the read statement to set the pointer, instead of moving the data into the
LongestRecordfield:You can now drop the declaration for the
LongestRecordvariable, and the statement settingpRecordto the address of that variable.For completeness, only: PL/I offers another way to map a storage area with two or more different layouts:
UNION, but this is not the question here.