delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/04/15:33:55

From: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: '...' handling (with djgpp, although likely ubiquitous)...
Date: 4 Aug 1997 04:52:24 GMT
Organization: The National Capital FreeNet
Lines: 49
Message-ID: <5s3n68$oto@freenet-news.carleton.ca>
References: <33e5c2e0 DOT 51066140 AT news DOT bc1 DOT com>
Reply-To: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
NNTP-Posting-Host: freenet3.carleton.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Burton Radons/Lothloriel, Hollow Dreams (lothloriel AT bc1 DOT com) writes:
> Base function:
> 
> char *rsprintf(const char *format, ...)
> {
>   va_list args;
>   char *buffer;
>   /* and on and on... */
> }

whatevertype myfunction (whatever, whatever, whatever, ...) {
  // One of the whatevers should provide the function with enough info to
  // know how many parameters to expect
va_list param_pt;                           // Declare the arg list variable
   va_start(param_pt, number);              // Call the setup macro

   cout << "The parameters are ";           // Example code that does something
   for (int index = 0 ; index < number ; index++)  // with params
   { 
      cout << va_arg(param_pt, int) << " "; // Extract a parameter
   }
   cout << "\n";
   va_end(param_pt);                        // Closing macro
}

There are five parts to varargs use. Part one: ... in the args of the
function, at the end. Part two: declaring a variable of va_list type.
Part three: determining the number of arguments (printf looks for the
number of format specifiers in the format string for instance) and calling:
va_start(the_va_list_variable, number_of_arguments_expected)
Part four: extracting a parameter. This involves determining what data
type to expect and using the value of:
va_arg(the_va_list_variable,the_expected_data_type)
You may be thinking "I never saw a function that took a type for an arg
before!" You still haven't; it's actually a macro in stdarg.h, which you
should #include.
Part five: after you are done with arguments, call:
va_end(the_va_list_variable)
and that's it.

Also, try looking at the source for such library functions as printf,
which use varargs.

--
    .*.  Where feelings are concerned, answers are rarely simple [GeneDeWeese]
 -()  <  When I go to the theater, I always go straight to the "bag and mix"
    `*'  bulk candy section...because variety is the spice of life... [me]
Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019