Mail Archives: djgpp/1997/11/30/01:18:13
BlueShade wrote:
> 
> First, I was wondering if anyone could tell me how to access "ellipses
> variables".  An example of what I'm babbling about is as follows.  If I
> define a function:
> 
>         void something( int x, int y, ... );
> 
> How do I access the variables that come in the ...?  Well, that's all
> for now...
Ah, the famous ellipsis.  You can access the variables represented by
the '...' using a special set of macros called va_args, and a special
set of display functions designed to handle them.  For example, here's a
small wrapper for printf() that echoes its output to a file:
#include <stdio.h>
#include <stdarg.h>
int my_printf( FILE *fp_echo, const char *fmt, ... )
{
    va_list v;
    int r;
    va_start( v, fmt );
    vfprintf( fp_echo, fmt, v );
    va_end( v );
    va_start( v, fmt );
    r = vprintf( fmt, v );
    va_end( v );
    return r;
}
There is also a macro named va_arg() that returns successive arguments
of a va_list, given the data type of each argument.  You must determine
what each argument is before calling va_arg().  The following code is
tested and works:
#include <stdio.h>
#include <stdarg.h>
void var_ints( int howmany, ... )
{
    va_list v;
    int i;
    va_start( v, howmany );
    for ( i = 0; i < howmany; i++ )
        printf( "%d ", va_arg( v, int ) );
    va_end( v );
    printf( "\n" );
    return;
}
int main( void )
{
    var_ints( 2, 3, 4 );
    var_ints( 4, 10, 8, 4, 11 );
    var_ints( 0 );
    var_ints( 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
    return 0;
}
For some reason, the documentation for the va_* macros was omitted from
the libc.inf for DJGPP v2.01.  Hopefully it'll be in the next version.
hth!
-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "A 'critic' is a man who creates     |
|       aka Fighteer I       | nothing and thereby feels qualified  |
|   mailto:fighteer AT cs DOT com   | to judge the work of creative men."  |
| http://www.cs.com/fighteer |               - Lazarus Long         |
---------------------------------------------------------------------
- Raw text -