Another Point on Pointers, and a Word About Education

Before delving into this months topic, a quick follow-up on last months column: A couple of readers noted that the simple application of a pointer provided an alternative solution. While we considered this approach (honestly, we did), we decided against using it. However, these responses show that this technique is in use among some RPGers, so we've decided to share it with the rest of you.

The essence of this approach is the use of a pointer to re-map the relevant part of the DS as an array. As you'll see, the code itself looks very similar to our first example. As before, the DS SalesData is defined based on the external description (x). However, the array SalesForQtr, instead of being part of the DS, is set up as a stand-alone array (y) based on the pointer pSalesForQtr. By using the BASED keyword, we're telling the compiler not to allocate storage for the array. Subsequently, by initializing the pointer with the address of field Q1 (z), we cause the array to be mapped over the fields Q1 through Q4.

(x)  D SalesData     E DS                  ExtName(TestFile)

     // Define array over Quarterly Sales figures

(y)  D SalesForQtr   S                     Based(pSalesForQtr)
    D                                     Like(Q1) Dim(4)

(z)  D pSalesForQtr  S                 *   Inz(%Addr(Q1))

Note that the externally described data structure used in this example is not optional. Don't be tempted to think that you can simply base the pointer on the Q1 field in the record format. This won't work because RPG doesn't guarantee that fields in a record format will be laid out contiguously in memory unless a DS is used.

This approach is quite clean, and a thorough understanding of pointers isn't required to use it. Of course if you encounter problems, a little more knowledge may come in handy. So check out our article to learn a little more about pointers. An excellent introduction to pointers is also available at Scott Klements web site.

On to the Next Topic: Education
In preparing this column, we were reminded of how the pressure to meet deadlines can easily overwhelm us all. This week's
COMMON conference and the unofficial start of the "conference season" brings another set of deadlines. Somehow, despite the fact that many of the conferences are months apart, everyone seems to be looking for their handouts at the same time. At least it feels that way right now. Of course, for founding members of the "Last-Minute Club" like us, this is hardly a new situation.


Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

comments powered by Disqus



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters