× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




On 11/01/2006, at 1:45 AM, Peter Grace wrote:

Both explanations do help me with a segue question I was going to ask,
which is what is the proper way to pass data structures back and forth
between C and RPG.  I had a suspicion that struct would come into play,
since in my mind as a C programmer, seeing an RPG DS just looks like a
struct to me!

If you really do want to pass data structures back and forth then you should do that using a bi-directional parameter just as you would in C.

Rather than returning the structure from the C function (via return) just accept the address of the structure into the C function and change it in place.

RPG passes by reference (unless told otherwise) and C passes by value (unless told otherwise). Passing a variable by reference is the same as passing a pointer by value therefore you can satisfy both the RPG view of the world and the C view by suitable prototyping. For example:

C view of the structure:
typedef _Packed struct { int fld1;
                         char fld2[25];
                         int fld3;
                       } foo_t;

RPG IV view of the structure:
D foo           DS                           QUALIFIED INZ
D   fld1                              10I 0
D   fld2                              25
D   fld3                              10I 0

Note the use of _Packed to force C to stop natural alignment. RPG does not align unless told to do so. So either use _Packed on C structs or ALIGN on RPG data structures.

void doFoo( foo_t* foo )
{
   foo->fld1 = 10;
   foo->fld3 = 42;
memset( foo->fld2, 0x40, sizeof(foo->fld2) ); /* blank pad for caller */
   memcpy( foo->fld2, "SOME TEXT", 9 ); /* Omit null-terminator */
}

D foo             DS                  QUALIFIED
D  fld1                         10I 0
D  fld2                         25
D  fld3                         10I 0
D doFoo           PR                  EXTPROC('doFoo')
D   foo                               LIKEDS(foo)
C                   CALLP     doFoo( foo )
C     foo.fld1      DSPLY
C     foo.fld2      DSPLY
C     foo.fld3      DSPLY
C                   SETON                                        LR
C                   RETURN                                          

This technique works with any variable type and can make the lives of both types of programmer much happier. However nothing seems to stop C programmers from objecting to fixed-length strings. Yet another of the afflictions created by K&R.


Regards,
Simon Coulter.
--------------------------------------------------------------------
   FlyByNight Software         AS/400 Technical Specialists

   http://www.flybynight.com.au/
   Phone: +61 3 9419 0175   Mobile: +61 0411 091 400        /"\
   Fax:   +61 3 9419 0175                                   \ /
                                                             X
                 ASCII Ribbon campaign against HTML E-Mail  / \
--------------------------------------------------------------------




As an Amazon Associate we earn from qualifying purchases.

This thread ...

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.