cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstdarg (stdarg.h) : va_start
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdarg (stdarg.h)
macros:
· va_arg
· va_end
· va_start
types::
· va_list

-

va_start macro
void va_start ( va_list ap, paramN );
<cstdarg>

Initialize a variable argument list

Initializes the object of type va_list passed as argument ap to hold the information needed to retrieve the additional arguments after parameter paramN with function va_arg.

A function that executes va_start, shall also execute va_end before it returns.

Parameters

ap
Object of type va_list that will hold the information needed to retrieve the additional arguments with va_arg.
paramN
Parameter name of the last named parameter in the function definition.

Return Value

none

Example

/* va_start example */
#include <stdio.h>
#include <stdarg.h>

void PrintFloats ( int amount, ...)
{
  int i;
  double val;
  printf ("Floats passed: ");
  va_list vl;
  va_start(vl,amount);
  for (i=0;i<amount;i++)
  {
    val=va_arg(vl,double);
    printf ("\t%.2f",val);
  }
  va_end(vl);
  printf ("\n");
}

int main ()
{
  PrintFloats (3,3.14159,2.71828,1.41421);
  return 0;
}

The function PrintFloats takes as its first parameter, amount, the number of additional arguments, which are then read using the cstdarg macros and printed out with a specific format.

See also

va_arg Retrieve next argument (macro)
va_end End using variable argument list (macro)
va_list Type to hold information about variable arguments (type)
vsprintf Print formatted variable argument list to string (function)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us