Using pointers to process a chained list

When you need to pass and receive addresses of record areas, you can use pointer data items, which are either data items that are defined with the USAGE IS POINTER clause or are ADDRESS OF special registers.

About this task

A typical application for using pointer data items is in processing a chained list, a series of records in which each record points to the next.

When you pass addresses between programs in a chained list, you can use NULL to assign the value of an address that is not valid (nonnumeric 0) to a pointer item in either of two ways:

  • Use a VALUE IS NULL clause in its data definition.
  • Use NULL as the sending field in a SET statement.

In the case of a chained list in which the pointer data item in the last record contains a null value, you can use this code to check for the end of the list:


IF PTR-NEXT-REC = NULL
. . .
   (logic for end of chain)

If the program has not reached the end of the list, the program can process the record and move on to the next record.

The data passed from a calling program might contain header information that you want to ignore. Because pointer data items are not numeric, you cannot directly perform arithmetic on them. However, to bypass header information, you can use the SET statement to increment the passed address.

Example: using pointers to process a chained list

Related references    
ADDR
  
SET statement (COBOL for Linux® on x86 Language Reference)