Anonymous user
Pointers and references: Difference between revisions
→{{header|COBOL}}: Syntax highlighting.
(→{{header|jq}}: special case) |
imported>Acediast (→{{header|COBOL}}: Syntax highlighting.) |
||
Line 494:
===Pointers===
Pointers are declared like so, optionally with the type or program they will point to:
<syntaxhighlight lang="
<code>USAGE POINTER</code> data items are used in conjunction with <code>BASED</code> data items, with <code>ALLOCATE</code> optionally giving a pointer the address of the allocated memory. Pointers can also be used to free allocated memory, which will cause the pointer to be set to <code>NULL</code>.
<syntaxhighlight lang="
PROCEDURE DIVISION.
FREE ptr</syntaxhighlight>▼
ALLOCATE heap-item RETURNING ptr
*> ...
▲ FREE ptr</syntaxhighlight>
<code>USAGE PROGRAM-POINTER</code> data items are used to point to programs and their entry points.
{{works with|OpenCOBOL}}
{{works with|Visual COBOL}}
<syntaxhighlight lang="
Both types of pointer support basic pointer arithmetic.
<syntaxhighlight lang="
SET ptr2 DOWN BY LENGTH OF foo</syntaxhighlight>
Pointers can also be set to point to where other pointers are pointing or to other pointers themselves.
<syntaxhighlight lang="
SET ptr2 TO ADDRESS OF ptr3 *> ptr2 points to ptr3</syntaxhighlight>
To alter the value pointed to by a pointer, the <code>SET</code> statement is needed once again and is used to set the address of <code>BASED</code> or <code>LINKAGE SECTION</code> data items, which can then be used to modify the data.
<syntaxhighlight lang="
MOVE "bar" TO foo</syntaxhighlight>
===References===
Object references are declared like so, optionally with the class/interface they will reference:
<syntaxhighlight lang="
They contain either a reference to an object or <code>NULL</code>.
They are initialised using by invoking a class constructor, and set using the <code>SET</code> statement.
<syntaxhighlight lang="
SET another-obj-ref TO obj-ref</syntaxhighlight>
|