Tips and Techniques: Declare Overlapping Data Structures

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Overlapping data structures are the most needed feature in a program, but they can also be useful when processing APIs or reading multi-format files.

But how do you declare overlapping data structures? Unfortunately, you'll need to use pointers. Fortunately, you won't have to do any pointer manipulation.

To declare a multi-format data structure, you have to declare each data structure independently. This means you declare them as you normally would. For example, Figure 1 contains a data structure named GTIN, and Figure 2 contains a data structure named UPC.


.....DName+++++++++++EUDS.......Length+TDc.Functions++++
     D GTIN            DS
     D  SeqNbrEx                      2S 0
     D  MerchID                       6S 0
     D  SeqNbr                        5S 0
     D  CHKDIGIT                      1S 0

Figure 1: GTIN Data Structure

.....DName+++++++++++EUDS.......Length+TDc.Functions++++
     D UPC             DS                
     D  MerchID                       6S 0
     D  SEQNBR                        5S 0
     D  CHKDIGIT                      1S 0

Figure 2: UPC Data Structure

At this point, both data structures have their own memory; they are not located in the same memory space. So you have to change them in order to provide a multi-format structure.

The change required is relatively slight; you need to add the BASED keyword to one of the data structures--specifically, to the data structure that is the shorter of the two. This means the UPC data structure would be changed to the following:

.....DName+++++++++++EUDS.......Length+TDc.Functions++++
     D UPC             DS                  Based(pGTIN)
     D  MerchID                       6S 0
     D  SEQNBR                        5S 0
     D  CHKDIGIT                      1S 0

In addition, you need to create the pGTIN pointer that is referenced by the BASED keyword. While declaring this pointer variable isn't strictly required (RPG automatically declares one for you), it is necessary to set this pointer to the address of the GTIN data structure. One way to do that is by explicitly declaring the pointer and initializing it to the address of the GTIN structure:

.....DName+++++++++++EUDS.......Length+TDc.Functions++++
     D pGTIN           S              *    Inz(%Addr(GTIN))

To provide the complete picture, I've reproduced the entire set of statements in Figure 3.

.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++++++++
     D pGTIN           S              *    Inz(%Addr(GTIN))

     D GTIN            DS                  QUALIFIED
     D  SeqNbrEx                      2S 0
     D  MerchID                       6S 0
     D  SeqNbr                        5S 0
     D  CHKDIGIT                      1S 0

     D UPC             DS                  QUALIFIED Based(pGTIN)
     D  MerchID                       6S 0
     D  SEQNBR                        5S 0
     D  CHKDIGIT                      1S 0

Figure 3: Multi-Format Data Structures

Now, these two data structures overlap one another in memory. Actually, the GTIN data structure has its own memory, and the UPC data structure provides access to the GTIN's memory via the pGTIN pointer variable. I've added the QUALIFIED keyword to avoid name collision. If you are still on OS/400 V4, QUALIFIED is not available, so you will need to ensure that the subfield names are unique in each data structure.

The storage for these two data structure would be formatted as illustrated in the following table.

Byte
Position
1
2
3
4
5
6
7
8
9
10
11
12
13
14
GTIN
SeqNbrEx
MerchID
SeqNbr
ChkDigit
UPC
MerchID
SeqNbr
ChgDigit

Data->














Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: