The C function uses the following macros in the program:
void Va_start (va_list arg_ptr, prev_param);
type Va_arg (va_list arg_ptr, type);
void Va_end (va_list arg_ptr);
va_list:a type of information required to hold macro va_start,va_arg , and va_end . In order to access parameters in a variable-length parameter list, you must declare
an object definition for the va_list type: typedef char * va_list;
Va_start:accesses the macro used before parameters in the variable-length argument list, initializes the object declared with va_list , initializes the result for the macro va_arg and
va_end use;
Va_arg: expands the macro into an expression that has the value and type of the next parameter in the variable-length argument list. Each call to Va_arg will be modified
The object declared with va_list so that the object points to the next parameter in the argument list;
Va_end:This macro enables the program to return normally from a variable-length argument list with a function referenced by the macro va_start .
va here is the meaning of the variable-argument(variable parameter).
These macros are defined in stdarg.h , so a program that uses mutable parameters should contain this header file. Let's write a simple variable parameter function, change the function to have at least one integer parameter, the second argument is an integer, is optional. The function simply prints the values of both parameters.
Problem Description:
The variable parameter list is used to find the maximum value of n shaping number and output.
The code is as follows:
/******** variable parameter list *********/#include <stdio.h> #include <stdarg.h>/ * ANSI standard form of declaration, the ellipsis in parentheses denotes an optional parameter */ int Max (int n,...) /* The maximum value of the n number */{va_list arg;/* defines the structure of the Save function parameter */int max=0;int i;va_start (arg,n); /* ARGP points to the first optional parameter passed in, MSG is the last determined parameter */for (i = 0;i<n;i++) { int tmp = VA_ARG (arg,int); /* Remove the current parameter, type int type */if (Tmp>max) {max = tmp;}} Va_end (ARG); /* End variable parameter get */return max;} int main () {int ret = Max (10,1,2,3,4,5,6,7,8,9,10);p rintf ("%d\n", ret); return 0;}
(c language) variable parameter list