• Subject: Re: C like Data Structure Definitions?
  • From: Scott Klement <klemscot@xxxxxxxxxxxx>
  • Date: Wed, 10 Jan 2001 00:36:29 -0600 (CST)


Hi Jim,

On Tue, 9 Jan 2001, Jim Langston wrote:

> In the C language,  you can create a data structure, called a 
> structure, then declare variables to use this structure.  Something
> like:
> 
> Struct  MyStruct {
>    Int      MyInt;
>    Long     MyLongInt;
>    Char[10] MyString;
>    };
> 
> MyStruct MyNewStruct;
> MyStruct MyNewStruct2;
> 
> (I may have the syntax wrong on that last one, it might be
> Struct MyStruct MyNewStruct; )
> 
> which means I now have two structures, MyNewStruct and MyNewStruct2 with
> the same definitions.  I want to do something similar in RPG, can I?
> 
> I am using in a copy source a data structure called RNF_Data.
> 

Yeah, C's structures are a lot nicer than RPG's, IMHO.  There are a few
approaches to solving your problem.

A) Externally define your data structure.   When you want to put it 
          into your program, you can do it like this:

          D MyNewStruct   e ds                  EXTNAME(MYSTRUCT)
          D                                     PREFIX(ds1)
          D MyNewStruct2  e ds                  EXTNAME(MYSTRUCT)
          D                                     PREFIX(ds2)

          This is more awkward, IMHO, than C's method.  On V3R2, I'm
          limited to 10-char names, which makex using "prefix" even
          more awkward than this already is :)

B) Use multiple occurrance data structures...  use named constants to
          keep track of the different occurrances.

          D MyStruct        DS                  OCCURS(5)
              ... add your subfields here ...
          D MyNewStruct     C                   CONST(1)
          D MyNewSTruct2    C                   CONST(2)

          Biggest problem with this approach is you have to remember
          to use OCCUR before accessing the subfields, and you'll
          run into problems when you try to make one be local to
          a subprocedure, or try to put the MODS in a /COPY member
          since each program will need a different number of occurances.

C) Use pointer-based structures in a /COPY file, and then define a local
          variable for each copy of the structure you want, and point
          the DS at the local var.

          Like so:

          (in /COPY member:)
          D mystruct      ds                    BASED(p_mystruct)      
             ... subfields here....
          D p_mystruct    S               *

          (in proc where you use it:)
          D/COPY SOURCE,MEMBER
          D MyNewStruct   S             16A  (or however big the DS is)
          D MyNewStruct2  S             16A  (or however big the DS is)
          c                   eval      p_mystruct = %addr(mynewstruct)
          c     ... work with vars in mynewstruct ...
          c                   eval      p_mystruct = %addr(mynewstruct2)
          c     ... work with vars in mynewstruct2 ...


Personally, I find myself using "C".  I like putting my structures in the
same /COPY member as the prototypes that they correspond with.  Most
people, however, aren't as comfortable with pointers as I am.

Option "A" is probably the best way to go, but I just hate having to
create many DDS members for what SHOULD all fit in one /COPY file, and
then having objects sitting out on disk just to use for the external
defs...   It upsets my sense of neatness.   However, I think you'll find
that almost everyone will tell you to do it this way, and they'll hate
my pointer method. :)

I sure hope they solve this problem in V5R1. :)    (I think we can agree
that this is one way in which RPG _should_ be more like C)

HTH


+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.