Your foo variable is declared as automatic storage and is local to the
getStr function.  As such, pointer you return will be valid, but the very
next instruction may very well overwrite this storage.
One way to solve it would be to declare foo as static (global) storage:

0001.00 #include <stdio.h>
0002.00 #include <string.h>
0003.00 #include <stdlib.h>
0005.00 char foo[25];
0007.00 char* getStr(int incomingVar)
0008.00 {
0009.00  sprintf(foo,"%d",incomingVar); 
0010.00  return foo;
0011.00 } 

Now the pointer you return will point to valid storage.


-----Original Message-----
[] On
Behalf Of Peter Grace
Sent: Monday, January 09, 2006 4:00 PM
To: c400-l@xxxxxxxxxxxx
Subject: [C400-L] Question regarding passing variables to ILE-RPG from C


I've been fiddling with C, modules, bound service programs, etc, and 
have come to a situation where I need the expertise of someone a bit 
more knowledgeable.

Given the following C Source:

0001.00 #include <stdio.h>
0002.00 #include <string.h>
0003.00 #include <stdlib.h>
0005.00 char* getStr(int incomingVar);
0007.00 char* getStr(int incomingVar)
0008.00 {
0009.00  char foo[25];
0010.00  sprintf(foo,"%d",incomingVar);
0011.00  return foo;
0012.00 }

And given the following ILE RPG Source:

0001.00 D getStr          PR            25A   extproc('getStr')
0002.00 D                                5I 0 VALUE
0003.00 C                   MOVEL     *BLANKS       FOOSTR           25
0004.00 C                   Eval      FOOSTR = getStr(1)
0005.00 C                   Eval      *INLR = *ON

(I've compiled the C source as a module, then created a service program 
using crtsrvpgm -- kudos to IBM for having an example of how to write a 
QSRVSRC file!)

I was expecting when I ran the rpg program, named USESTR, that FOOSTR 
would equal something akin to '1                 ' -- that is, the 
visual depiction of 1 and 24 other spaces.  I was greeted instead by 

I think that this has something to do with the pointer that I'm creating 
doesn't exist outside of the service program?  Is this correct, or am I 
just making a big goof?

What is the "Right Way" for me to return a char* array (or even a data 
structure) via a bound-C service program to a RPG program?  Is there 
even a right way to do it?

Any help would be appreciated!

Peter Grace

This thread ...


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

This mailing list archive is Copyright 1997-2020 by 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].