Variable Function Parameter Parsing in C Language
Most of the time, the number of formal parameters in a function is usually determined. During the call, all actual parameters corresponding to the formal parameters must be given in sequence. However, in some cases, the number of parameters of the function can be determined as needed. Typical examples include:
Familiar functions: printf () and scanf ()
Variable Parameter implementation:
C Language header file stdarg. h provides a data type va-list and three macros (va-start, va-arg, and va-end). va-start points vp to the first optional parameter. Va-arg returns the current parameter in the parameter list and directs vp to the next parameter in the parameter list. Va-end clears the vp pointer to NULL. The function can traverse these parameters multiple times, but all parameters must start with va-start and end with va-end. They are used to test the variable parameter table when the called function does not know the number and type of parameters. This provides a convenient and effective way to access variable parameters. Va-list is a pointer of the char type. When the called function uses a variable parameter, it declares a variable of the va-list type, this variable is used to point to the locations of information required by va-arg and va-end. The following is the source code of va_list in C: typedef char * va_list;
When the ANSI standard form is used, the prototype declaration of a function with a variable number of parameters is:
Type funcname (type para1, type para2 ,...) this form requires at least one common form parameter, and the ellipsis behind it does not mean to be omitted, but is part of the function prototype. type is the type of the function return value and form parameter. When a caller calls a function with a variable number of parameters, the caller must specify the number of actual parameters in a certain way, for example, setting the last parameter to a null string. The following is a specific example:
Calculate the sum of n numbers:
int Sum (int n, ...)
{
int i = 0, sum = 0;
va_list vp;
va_start (vp, n); // va-start makes vp point to the first optional parameter
for (i = 0; i <n; i ++)
{
sum + = va_arg (vp, int);
}
va_end (vp); // va-end clears the vp pointer to NULL.
return sum;
}
Printf function implementation: (only printing of basic types is implemented here)
1 void my_print (const char * format, ...)
2 {
3 char c = 0;
4 va_list vp;
5 va_start (vp, format); // vp points to the first optional parameter
6 while (* format)
7 {
8 c = * format;
9 switch (c)
10 {
11 case '%':
12 {
13 char cc = * (++ format);
14 switch (cc)
15 {
16 case 'd':
17 {
18 char str [50];
19 int n = va_arg (vp, int); // va—arg returns the current parameter in the parameter list and makes vp point to the next parameter in the parameter list.
20 char * string = _itoa (n, str, 10); // Convert integer to string
21 print_str (string);
twenty two }
23 break;
24 case 'f':
25 {
26 char str [50];
27 double f = va_arg (vp, double); // va—arg returns the current parameter in the parameter list and makes vp point to the next parameter in the parameter list
28 char * string = _gcvt (f, 10, str); // Convert floating point number to string, rounded
29 print_str (string);
30}
31 break;
32 case 'c':
33 putchar (va_arg (vp, char));
34 break;
35 case 's':
36 {
37 char * string = va_arg (vp, char *);
38 print_str (string);
39}
40 break;
41 default:
42 break;
43}
44}
45 break;
46 default:
47 putchar (c);
48 break;
49}
50 format ++;
51}
52 va_end (vp);
53}
Understanding variable parameters is very important to understand the function stack frame creation and destruction, which involves the parameter is how to press the stack, the content of this blog: http://www.cnblogs.com/zhonglongbo/p/8392026.html