Simple pointer operations
For most parameters passed to COBOL RAM function programs, a small
amount of pointer dereferencing code may need to be implemented using
the SET
operator. For example, most programs will receive
a pointer to a 256-byte buffer for a detailed error message. Before
you can fill this buffer though, it must be dereferenced using the SET
operator.
For smaller items, dereferencing can be avoided by USING BY REFERENCE
.
As an example, the following code demonstrates how to establish
addressability to the error buffer. The pointer to the error buffer
is passed by value to the procedure division for getInstances
and
is defined in the linkage section as follows:
77 GIP-ERROR POINTER.
Later in the linkage section, a 77-level item is defined for dereferencing and performing operations on the error buffer:
77 ERROR-BUFFER PIC X(256).
Then, within the procedure division we establish addressability
to the error buffer after verifying that GIP-ERROR
is not
NULL:
SET ADDRESS OF ERROR-BUFFER TO GIP-ERROR.
Now we can treat the error buffer as we would any normal 256-byte alphanumeric field. In this case, the error buffer is a 256-byte non-NULL-terminated string.