C Language: va_start function
(Fetch Argument from Variable Argument List)
In the C Programming Language, the va_start function initializes the variable argument list referred to by ap. The va_start function must be called before using the va_arg function.
Syntax
The syntax for the va_start function in the C Language is:
void va_start(va_list ap, parmN);
Parameters or Arguments
- ap
- A variable argument list.
- parmN
- The name of the last ordinary parameter.
Returns
The va_start function does not return anything.
Required Header
In the C Language, the required header for the va_start function is:
#include <stdarg.h>
Applies To
In the C Language, the va_start function can be used in the following versions:
- ANSI/ISO 9899-1990
va_start Example
/* Example using va_start by TechOnTheNet.com */ #include <stdio.h> #include <stdarg.h> int add(int n, ...) { /* Define temporary variables */ va_list list; int total; /* Initialize total */ total = 0; /* Set where the variable length part of the argument list ends */ va_start(list, n); /* Loop through each argument adding the int values */ for (int i=0; i < n; i++) total = total + va_arg(list, int); /* Clean up */ va_end(list); /* Return the calculated total */ return total; } int main(int argc, const char * argv[]) { /* Define temporary variables */ int value1, value2, value3; int result; value1 = 3; value2 = 4; value3 = 5; /* Call the add function */ result = add(4, value1, value2, value3); /* Display the results of the additon */ printf("The sum of %d, %d and %d is %d\n", value1, value2, value3, result); return 0; }
When compiled and run, this application will output:
The sum of 3, 4 and 5 is 12
See Also
Other C functions that are noteworthy when dealing with the va_start function:
Advertisements