Implementation of variable parameters in C language functions
How to implement variable parameters for a function:
Since function Overloading is not available in C language, it is difficult to solve the problem of variable number function parameters. Even if C ++ is used, it is difficult to use function overloading if the number of parameters cannot be determined. In this case
And puts forward pointer parameters to solve the problem.
(1) va_list
A pointer arg_ptr is defined to indicate optional parameters.
(2) va_start (arg_ptr, argN)
Make the parameter list pointer arg_ptr point to the first optional parameter in the function parameter list. argN is a fixed parameter located before the first optional parameter, or the last fixed parameter. If there is a va
The function declaration is void va_test (char a, char B, char c ,...), then its fixed parameters are a, B, c, and the last fixed parameter argN is c, so it is va_start.
(Arg_ptr, c ).
(3) va_arg (arg_ptr, type)
Return the parameter indicated by arg_ptr In the parameter list. The return type is type. Point arg_ptr to the next parameter in the parameter list. Optional parameters are returned, excluding fixed parameters.
(4) va_end (arg_ptr)
Clear the parameter list. The parallel parameter pointer arg_ptr is invalid.
(Note: va here refers to variable-argument (variable parameters). These macros are defined in stdarg. h, so programs that use variable parameters should include this header file)
The following describes the situation with a few chestnuts:
Example 1:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdarg.h> 4 5 int print(const char *format, ...) 6 { 7 va_list args; 8 const char *args1; 9 va_start(args, format);10 args1 = va_arg(args,const char *);11 va_end(args);12 printf("format=%s args1=%s", format, args1);13 }14 int main()15 {16 print("11111", "22222");17 }
Running result:
1 /*2 format=11111 args1=222223 Process returned 24 (0x18) execution time : 0.542 s4 Press any key to continue.5 */
Example 2:
Example 1 2: 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <stdarg. h> 5 6 int print (const char * format ,...) 7 {8 va_list args; 9 const char * args1; 10 va_start (args, format); 11 args1 = va_arg (args, const char *); 12 va_end (args ); 13 printf ("format = % s args1 = % s", format, args1); 14} 15 16 int main () 17 {18 print ("11111", "22222 ", "333333"); 19}
Running result:
1 /*2 format=11111 args1=222223 Process returned 24 (0x18) execution time : 0.542 s4 Press any key to continue.5 */
We can see from the above that adding three parameters to the function is no problem;
Example 3:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdarg.h> 4 5 int print(const char *format, ...) 6 { 7 va_list args; 8 const char *args1; 9 va_start(args, format);10 args1 = va_arg(args, const char *);11 args2 = va_arg(args, const char *);12 va_end(args);13 printf("format=%s args1=%s", format, args1, args2);14 }15 16 17 int main()18 {19 20 print("11111", "23333");21 }
Running result:
1 /*2 format=11111 args1=233333 Process returned 24 (0x18) execution time : 0.153 s4 Press any key to continue.5 6 */
Note that there are no three parameters above, but the program can still run!
From the preceding three examples, we can see that all input parameters are of the same type;
Example 4:
Different types of function parameters:
1 int print(const char *format, ...) 2 { 3 va_list args; 4 int args1; 5 va_start(args, format); 6 args1 = va_arg(args, int); 7 va_end(args); 8 printf("format=%s args1=%d", format, args1); 9 }10 11 12 int main()13 {14 15 print("11111", 123);16 }
Running result:
1 /*2 format=11111 args1=1233 Process returned 22 (0x16) execution time : 0.007 s4 Press any key to continue.5 */
However, note that the following cannot be used:
In short, the type in va_arg (ap, type) cannot be of the following types:
-- Char, signed char, and unsigned char
-- Short, unsigned short
-- Signed short, short int, signed short int, unsigned short int
-- Float