One way or another, COBOL pointers can do almost anything that C pointers can, but more clumsily. In particular:
With pointers, however, COBOL's verbosity is a distinct disadvantage. You need two separate statements to access a variable through a pointer: one to set the address of a variable in the LINKAGE SECTION, and another to do something with that variable. This split into separate statements creates opportunities for mistakes, especially when they are widely separated by other statements.
In C, by contrast, access through a pointer is concisely expressed as a single operation.
This limitation seems arbitrary and unnecessary. Since you can pass a WORKING-STORAGE variable to a subprogram, the compiler can clearly take its address -- that's what it really passes. But it won't let you do the same.
If necessary, you can devise a simple workaround. Pass your WORKING-STORAGE variable to a subprogram which takes the address of one parameter and returns that address in another parameter.
Not so in COBOL. Here a pointer is not numeric in any sense. You can sort of increment a pointer by redirecting it to a piece of the thing it's already pointing to, but you can't decrement a pointer, nor can you compare two pointers except for equality.
(I've read that you can REDEFINE a pointer as an S9(9) COMP and do arithmetic on the redefinition. I haven't tried it. In any case this trick is not portable, since it makes assumptions about the physical representation of a pointer.)
In practice these limitations are not serious. Pointer arithmetic in C
is useful mainly when pointers point into arrays. In that context, it
is simpler in COBOL to use table indices or subscripts.
In COBOL, a pointer can point only to data. You may be able to simulate
a function pointer by passing an action code to a routine which inspects
the code and branches accordingly. For an even better simulation you
can use dynamically called subprograms. The name of the subprogram can
play roughly the same role as a function pointer.
[According to Raymond W. Leech III
(rleech@sprynet.com), Micro
Focus supports a PROCEDURE-POINTER extension, which presumably
corresponds to a function pointer in C. There may be similar constructs
in other dialects.]
There are no function pointers
In C, a pointer may point to a function. Such a pointer is a variable
like any other: you can assign a value to it, you can pass it as
a parameter, you can store it in a data structure. Most importantly,
you can invoke the function to which the pointer points. This facility
is sometimes useful despite the perplexing syntax.
Recommendation
Design your pointer operations using the more concise and manageable
syntax of C. When you're satisfied that your C-like pseudocode is
correct, translate it mechanically into the equivalent COBOL.
COBOL Home