/************************************************************************* > File name:va_list.c > Author:zs hh0604 > Mail: [email protected] > Created time:2014 October 14 Tuesday 15:16 09 sec. ***************************** /#include <stdio.h>/*** with variable parameters in C, a va_list type is provided in C to store the variable parameters of the function. 1. The variable parameter method for defining a function in the C language is as follows: first: The parameter must be at the end. Two: The last parameter must declare the type of the mutable parameter. Here are three: You can use three-point notation for parameters. Four: The parameter type and the number of parameters are set by using the FMT character parameter in the function call. such as: "%s%d%c%f" void Test (char *fmt,...); 2. If a variable parameter is used. C provides a set of functions as follows: void Va_start (Va_list ap, last), type Va_arg (va_list AP, type), void Va_end (va_list ap), Void Va_copy (va_list Dest, va_list src); operation on a mutable parameter. Function prototypes. void Va_start (va_list ap,last); function function: Points the AP in turn to the variable parameter. Function parameters: AP: Points to each variable parameter in turn. Last: Is the FMT, its role is to specify the parameters of the number of variable parameters, and the type. function return value: no return value. You must first call the Va_start () function before calling the group function. Formats the characters in the FMT format. To get the value of the parameter at one time. Char *s;int D;char c;while (*fmt) {switch (*fmt++) {case ' s ': s = Va_arg (AP, char *); Break;case ' d ':d = Va_arg (AP, int); Case ' C ': c = Va_arg (AP, char); Va_end (AP);You must call the Va_end () function before the function ends. Use vsnprintf () directly with ap.vsnprintf (): The function prototype is as follows: int vsnprintf (char *str, size_t size, const char *format, va_list AP); as follows://Va_list function://Header file <stdarg.h> provides functionality to traverse the function parameter tables of unknown data and types. Then, declare a variable AP of type va_list in function f. It will point to each actual parameter in turn. Va_list ap;////You must initialize the AP once by using the Va_start macro before accessing any unnamed parameters. Va_start (va_list ap,lastarg);////each subsequent macro va_arg will produce a value that has the same type and number as the next one that is not command. It also modifies the AP so that the next parameter is returned the next time the va_arg is executed. Type Va_arg (va_list AP, type),////void Ve_end (Va_list ap),////after all processed parameters have been processed. And before exiting F, you must call the macro Va_end once. void Va_end (Va_list ap); **//************************************************************************* > Fil E name:snprintf.c > author:zshh0604 > Mail: [email protected] > Created time:2014 year October 14 Tuesday 13:21 13 SEC ************************************************************************/#include <stdlib.h> #include < stdio.h> #include <stdarg.h>char * make_message (const char *FMT, ...) The {int n;/* is used to record the return of vsnprintf (). If this value is less than or equal to-1,Life. There is not enough space allocated. If this value is greater than-1, but N is not less than size, then the value should be added 1, (size+1), otherwise space will be increased by one ***/int size = 100; /* Guess We need no more than bytes. */char *p, *NP; Va_list ap;if ((p = malloc (size)) = = NULL) return Null;while (1) {/* Try to print in the allocated space. */va_start (AP, FM T); This function allows the AP to point to each parameter in a variable parameter. n = vsnprintf (p, size, FMT, AP); Format the parameters that the AP points to in P. The maximum size of p is sizes. Print in FMT format. Va_end (AP); After the use is finished, call Va_end (AP); function. /* If that worked, return the string. */IF (n >-1 && N < size) return p;/* Else try again with more space. */IF (n >-1)/* glibc 2.1 */size = n+1; /* Precisely what is needed */else/* GLIBC 2.0 */size *= 2; /* Twice the old size */if ((NP = ReAlloc (p, size) = = NULL) {free (P); return NULL;} else {p = NP;}}}
The processing of variable parameters of C-language function