× 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.


  • Subject: Re: Prototyping printf()
  • From: "paul cunnane" <paul@xxxxxxxxxxx>
  • Date: Tue, 26 Sep 2000 10:50:04 +0100

> The problem as I see it, is that RPG's prototypes have nothing that works
> the same was as the elipses (...) in C.   (Well, I guess "CALLB" is
> close, but is ugly, and not as powerful as prototyped calls.)

I've been having a quick look through the source for printf.  Without a
detailed knowledge of how a C compiler works, the whole ellipsis mechanism
looks kinda kludgy.

It seems that the compiler replaces the ellipsis with all the parameters
that were actually passed, mushed up together in a contiguous block of
memory, which is in turn contiguous with the last parameter before the
ellipsis.  The called function (e.g. printf) is responsible for figuring out
the contents of this block of memory, and for extracting the values.  The
macros va_start, va_arg and va_end (from stdarg.h) are used to do this.
va_start takes the last non-ellipsis argument (the format string in printf),
and points to the next byte of memory after that parameter.  va_arg returns
each argument in turn, using the variable type to determine the number of
bytes to return.  This is why the specifiers are required in the format
string: so that printf knows what types of arguments to look for.

Based on this, maybe it is possible to call printf from RPG: prototype the
function with two arguments; a *STRING and a pointer.  Allocate memory for
the arguments, pack them into this memory block, and pass the address to
printf.

D printf       pr       10i 0    extproc('printf')
D  format                 *      const options(*string)
D  args                   *

C       eval      args = NewArgs
C       callp     AddIntArg(args, intValue)
C       callp     AddCharArg(args, charValue)
C
C       callp     printf("%i, %c" : args)

Or something.  I (obviously!) haven't tried this.

--
Paul


+---
| 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 ...

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.