I've heard comments about some RPG IV programmers being "pointer challenged." This bothers me, because this data type has been available since RPG IV's arrival in late 1994, almost 11 years. The pointer concept has been around since the invention of the computer! This tip explains the basics of RPG IV pointers and includes some examples.
The concept of a pointer (in any language) is that some variable contains an address of something else, rather than some kind of data item or procedure. If I refer to a standalone variable by its name, such as "Field5," and Field5 has been defined as a character variable with length 5, then every time I use Field5 I am referring to the storage (memory) address assigned by the compiler and loader. You could say that Field5 "points" to the five characters set up in storage. The address associated with Field5 is fixed. In contrast, a pointer variable contains an address that can change.
In RPG IV, a pointer variable is defined using the internal data type code of *. A quick check of the compile listing shows that the compiler sets aside 16 bytes for each pointer. Even though the i5 (or iSeries) uses a 64-bit address (8 bytes), 16 bytes are still used. Maybe IBM is planning ahead for 128-bit addressing--who knows? A pointer variable can contain either a procedure address or the address of a data item. If the pointer contains a procedure address, the keyword PROCPTR is used on the pointer definition. A pointer that is used to "point to" a data item is called a "basing pointer." Variables, data structures, and arrays may be used with basing pointers, and if so, the BASED keyword is specified and a pointer name is the value of the keyword.
Here's an important concept: When a data item is based, there is no storage allocated to it. It is a definition with a "yet to be determined" home in storage. I like to think of a based data item as a clear plastic ruler (or template) that's floating above the storage plane. It cannot be used until the pointer associated with it has a storage address. The value in the basing pointer determines where the template will be laid in the storage plane. Here is a simple example:
D Phrase S 25 Inz('Peter Piper picked a Plum')
D Field5 S 5 Based(Ptr)
D Ptr S *
/free
// You can't use Field5 yet.
// Now set the pointer.
Ptr = %addr(Phrase);
// Ptr has a value, the memory address of
// Phrase (left-most byte). Field5 can now
// be used.
// The current value of Field5 is 'Peter'
// Now move the template (address in Ptr) to the right 6 bytes
Ptr += 6;
// The value of Field5 is now 'Piper'
// Change Field5.
Field5 = 'Smith';
// The value of Phrase is now 'Peter Smith picked a Plum'
/end-free
LATEST COMMENTS
MC Press Online