Pointers: COBOL vs. C

This page is a brief comparison of pointers in COBOL and pointers in C. It is intended for people who are familiar with both languages but not familiar with pointers in COBOL.

One way or another, COBOL pointers can do almost anything that C pointers can, but more clumsily. In particular:

COBOL pointers are untyped

A COBOL pointer is like a void pointer in C: it can point to any kind of object. In C, if you accidentally try to use a FOO * to access a BAR, the compiler will probably complain. Not so with COBOL.

The syntax is verbose and error prone

COBOL is famous for its verbosity. Usually this verbosity makes it easier to read than the cryptic gibberish of C.

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.

You can't take the address of just anything

In C you can take the address of anything: just put an ampersand in front of its name. But in COBOL II, you can only take the address of things in the LINKAGE SECTION (other dialects may not suffer from this limitation).

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.

You can't do pointer arithmetic

In C, you can add to a pointer (or subtract from it) to point it to a nearby location. Within limits you can test whether one pointer is greater than another, or take the difference between them.

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.

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.

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.]

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.
[home]COBOL Home