va_start

Initialize the variable-length argument list.

  #include <stdarg.h>
  
  void va_start(va_list ap, lastparm);    
Parameter

ap

A variable list

lastparm

The last named parameter.

Remarks

The va_start() macro initializes and assigns the argument list to ap. The lastparm parameter is the last named parameter before the ellipsis ( ...) in the function prototype.

Listing: Example of va_start Usage

#include <stdarg.h>

#include <stdio.h>

void multisum(int *dest, ...);

void multisum(int *dest, ...)

{

va_list ap;

int n;

int sum = 0;

va_start(ap, dest);

while ((n = va_arg(ap, int)) != 0)

sum += n; /* Add next argument to dest */

*dest = sum;

va_end(ap); /* Clean things up before leaving. */

}

int main(void)

{

int all;

all = 0;

multisum(&all, 13, 1, 18, 3, 0);

printf("%d\n", all);

return 0;

}

Output:

35