In COBOL, arrays are called tables. A table is a set of logically consecutive data items that you define in the DATA DIVISION by using the OCCURS clause.
Pointers are data items that contain virtual storage addresses. You define them either explicitly with the USAGE IS POINTER clause in the DATA DIVISION or implicitly as ADDRESS OF special registers.
Example to use ADDRESS OF:
01 WORK-AREA. 03 WK-DATA1 PIC X(4) VALUE 'AAAA'. 03 WK-DATA2 PIC X(4) VALUE 'BBBB'. 03 WK-DATA3 PIC X(4) VALUE 'CCCC'. 03 WK-DATA4 PIC X(4) VALUE 'ZZZZ'. 01 WK-PTR POINTER. LINKAGE SECTION. 01 TBL-DATA. 03 TBL-DATA1 PIC X(4). 03 NEXT-TBL-DATA PIC X(4). PROCEDURE DIVISION. SET WK-PTR TO ADDRESS OF WORK-AREA. SET ADDRESS OF TBL-DATA TO WK-PTR.
Example to SET POINTER:
ID DIVISION. PROGRAM-ID. PROGA. WORKING-STORAGE SECTION. 77 APTR USAGE POINTER. 77 APROC-PTR USAGE PROCEDURE-POINTER. 01 AB. 05 BPTR USAGE POINTER. 05 BVAR PIC S9(3) PACKED-DECIMAL. LINKAGE SECTION. 01 AVAR. 05 CVAR PIC X(30). PROCEDURE DIVISION. SET APTR TO ADDRESS OF AVAR. SET APROC-PTR TO ENTRY "PROGA".
You can perform the following operations with pointer data items:
- Pass them between programs by using the CALL . . . BY REFERENCE statement.
- Move them to other pointers by using the SET statement.
- Compare them to other pointers for equality by using a relation condition.
- Initialise them to contain an invalid address by using VALUE IS NULL.
Use of pointer data items to:
- Accomplish limited base addressing, particularly if you want to pass and receive addresses of a record area that is defined with OCCURS DEPENDING ON and is therefore variably located.
- Handle a chained list.