I am very interested in the mutable parameter list recently because it can accept different numbers of parameters. Let's take a look at its statement first:
void printf (const char *format,...)
We understand it through a concrete example:
#include <stdio.h> #include <stdarg.h>int average (int val, ...) {int i = 0;int sum = 0;va_list arg;//defines a pointer to a char type va_start (arg,val);//Read all parameters, start from scratch, read the number of arguments you give, and put the parameters in the pointer for (i = 0; Lt Val i++) {sum + = Va_arg (arg,int);} Va_end (ARG);//end read return sum/val;} int main () {int ret = 0;ret = average (5,1,2,3,4,5);p rintf ("%d\n", ret); return 0;}
The mutable parameter list is implemented by macro-stdarg, which declares a type va_list and three macro va_start,va_arg,va_end,by right-clicking and going to the definition, we can see that va_list is actually a char * rename, which is written in the function, typedef char * va_list, which is very well understood; for Va_start,va_arg and Va_ End three macros we go to the definition where we can see that va_start is actually like this: while _crt_va_start is still an identifier defined by define, it should actually be (AP = (va_list) _addressof (v) + _intsizeof (v)); Va_arg and Va_start are similar to identifiers defined by define, and its prototype should look like this: #define _CRT_VA_ARG (Ap,t) (* (t *) (AP + = _intsizeof (t))-_ Intsizeof (t))), the AP is the first parameter that points to my mutable parameter list, which can be found after the parameter, and for va_end its prototype should be such a # define _CRT_VA_END (AP) (AP = (va_list) 0), To end the list of arguments that Va_list said, when the last mutable parameter is accessed, we need to call Va_end to end the list of arguments that point to
This article is from the "10944002" blog, please be sure to keep this source http://chrisapril.blog.51cto.com/10944002/1765033
Variable parameter list