[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
#include <stdarg.h>
type va_arg( va_list param, type );
Description:
va_arg is a macro that can be used to obtain the next argument in a
list of variable arguments. It must be used with the associated macros
va_start and va_end. A sequence such as
void example( char *dst, ... )
{
va_list curr_arg;
int next_arg;
va_start( curr_arg, dst );
next_arg = va_arg( curr_arg, int );
.
.
.
causes next_arg to be assigned the value of the next variable argument.
The argument type (which is int in the example) is the type of the
argument originally passed to the function.
The macro va_start must be executed first in order to properly
initialize the variable curr_arg and the macro va_end should be
executed after all arguments have been obtained.
The data item curr_arg is of type va_list which contains the
information to permit successive acquisitions of the arguments.
Returns:
The macro returns the value of the next variable argument, according to
type passed as the second parameter.
Example:
#include <stdio.h>
#include <stdarg.h>
void test_fn( const char *msg,
const char *types,
... );
void main()
{
printf( "VA...TEST\n" );
test_fn( "PARAMETERS: 1, \"abc\", 546",
"isi", 1, "abc", 546 );
test_fn( "PARAMETERS: \"def\", 789",
"si", "def", 789 );
}
static void test_fn(
const char *msg, /* message to be printed */
const char *types, /* parameter types (i,s) */
... ) /* variable arguments */
{
va_list argument;
int arg_int;
char *arg_string;
const char *types_ptr;
types_ptr = types;
printf( "\n%s -- %s\n", msg, types );
va_start( argument, types );
while( *types_ptr != '\0' ) {
if (*types_ptr == 'i') {
arg_int = va_arg( argument, int );
printf( "integer: %d\n", arg_int );
} else if (*types_ptr == 's') {
arg_string = va_arg( argument, char * );
printf( "string: %s\n", arg_string );
}
++types_ptr;
}
va_end( argument );
}
produces the following:
VA...TEST
PARAMETERS: 1, "abc", 546 -- isi
integer: 1
string: abc
integer: 546
PARAMETERS: "def", 789 -- si
string: def
integer: 789
Classification:
ANSI
Systems:
MACRO
See Also:
va_end, va_start, vfprintf, vprintf, vsprintf
See Also: va_end va_start
Online resources provided by: http://www.ClipX.Net --- NG 2 HTML conversion by Dave Pearson