Adjusting the Value of Pointers

The following example shows you how to adjust the value of a pointer by increasing it UP BY or decreasing it DOWN BY an integer value. This method of changing the value of a pointer can be useful when you are accessing items in a table that is referenced by a pointer data item.

   WORKING-STORAGE SECTION.
    01 A.
       05 ARRAY-USER-INFO OCCURS 300 TIMES.
          10  USER-NAME PIC X(10).
          10  USER-ID   PIC 9(7).
       01 ARRAY-PTR USAGE IS POINTER.
   LINKAGE SECTION.
   01  USER-INFO.
       05 USER-NAME LIKE USER-NAME OF ARRAY-USER-INFO.
       05 USER-ID LIKE USER-ID OF ARRAY-USER-INFO.
   PROCEDURE DIVISION.
         SET ARRAY-PTR TO ADDRESS OF ARRAY-USER-INFO(200). 1
         SET ADDRESS OF USER-INFO TO ARRAY-PTR. 2
         SET ARRAY-PTR UP BY LENGTH OF USER-INFO. 3
         SET ADDRESS OF USER-INFO TO ARRAY-PTR. 4
         MOVE "NEW NAME" TO USER-NAME OF USER-INFO.5
Notes:
  1. The first SET statement places the address of the 200th element of the ARRAY-USER-INFO array into the pointer ARRAY-PTR.
  2. The second SET statement gives data item USER-INFO the same address as the 200th element of the ARRAY-USER-INFO array.
  3. The third SET statement increments the address contained in pointer ARRAY-PTR by the length of one element of the array.
  4. The fourth SET statement gives data item USER-INFO the same address as the 201st element of the ARRAY-USER-INFO array (in other words, up one element from the second SET statement).
  5. This move is the same as:
      MOVE "NEW NAME" to USER-NAME OF ARRAY-USER-INFO (201).

For a complete definition of the SET statement, refer to the ILE COBOL for AS/400 Reference.