MF 

Pointer Examples

Pointer dereferencing

The following excerpt demonstrates how to dereference a pointer using the :: (colon-colon) syntax and the DATA...AT syntax. it also demonstrates how subscripting and reference modification is used in such situations:

       01 type1 typedef.
         03 array1 pic x(5) occurs 3.
         03 buff1  pic x(10).

       01 n1 type1.
       01 p1 pointer type1.

       procedure division.

      * qualification
       move "abcde" to n1::array1(1)
       move "abcde" to n1::array1(2)(2:5)
       move "abcdef" to buff1 of n1

       set p1 to address of n1

      * pointer dereferencing
       display p1::array1(2)(2:1)
       display p1::buff1(3:2)
       display p1::data(8:3)

       display array1(2)(1:3) at p1 
       display array1(2) at p1
       display buff1(3:2) at p1
       display data(8:3) at p1

Pointer coercion

The following excerpt demonstrates pointer coercion: changing the type of a pointer.

Care must be taken using coercion. If the underlying data is not of the correct type, undefined behavior can occur.

       01 type1 typedef.                  
          03 component-1 pic x(10).       
          03 component-2 pic xxxx comp-5. 

       01 type2 typedef.                  
          03 component-1 pic x(10). 
          03 component-2 pic xxxx comp-5.

       01 p1 pointer type1.
       01 p2 pointer type2.
 
       01 grp1 type1.
       01 grp2 type2.

      *> The following would produce syntax errors without the AS

         set p1 as type2 to address of grp2
 
         set p2 to p1 as type2

         move p1 as type2::component-1 to grp1::component-1

         move component-1 at p1 as type2 to component-1 of grp1