Variable List of parameters

Source: Internet
Author: User

function prototypes, which list the parameters that the function expects to accept, but the function can only display a fixed number of parameters. Is it possible for a function to accept a different number of parameters at different times? The answer is yes (printf is a mutable function), but there are some limitations.

int avarage (int val, int v1, int v2, int v3,int v4, int v5) {       floatsum = v1;       if (val>= 2)              sum+= v2;       if (val>= 3)              sum+= v3;       if (val>= 4)              sum+= v4;       if (val>= 5)              sum+= v5;       Returnsum/val;}


Obviously this is a bad example, this function has a few problems: first, it does not correct the number of parameters to test, can not detect too many of such cases. Just this problem easy to solve, simply add test. Second, a function cannot handle more than 5 values. To solve the problem, you just have to add some similar code to the already bloated code. In addition, there is a more serious problem: that the actual parameters and the corresponding shape is not necessarily accurate.

Called when the Avarage (3,1,2,3);

But there will be a mistake.
Error C2660: ' avarage ': function doesnot take 3 parameters

In order to solve the above problems:

The concept of variable list is introduced in C language.

STDARG macro

Variable list is implemented by macro, which is defined in the Stdarg.h header file, which is part of the standard library.

For example, the following programs:

The average value of the specified number of values  2 #include <stdarg.h> 3 float (int values,...) 4 {5    va_list   var_arg; 6    int count; 7
   float sum=0; 8     9    var_start (var_arg,n_values);//prepare to visit the variable number     for (count=0;count<n_values;count+=1)//Add the value from the variable tables Each     {        sum+=var_varg (var_arg,int);     }14     var_end (var_arg);       Completed processing variable number of parameters 15     


There are a few variables that need to be explained. va_list , va_start () , va_end and va_arg

va_list This type of variable is used to access variable parameters, which is actually a pointer.

va_start (): Span style= "font-family: Arial" > is a VL points to the first variable parameter, called after use is complete va_end () end.

Va_end : Also a macro, used to end Va_start () of the call.

Va_arg : A macro that is used to remove a value from the list of references.

Declares a variable arg of type va_list, which is used to access the indeterminate part of the list of references. This variable is initialized with the call to Va_start. Its first argument is the variable name of Va_list, and the 2nd parameter is the last named parameter before the ellipsis. The initialization process sets the VAR_ARG variable to the first parameter that points to the variable parameter part.

In order to access the arguments, you need to use VA_ARG, which accepts two parameters: the type of the next argument in the Va_list variable and the list of references. In this example, all of the variable parameters are integral types. Va_arg returns the value of this parameter and uses Var_arg to point to the next variable parameter.

Finally, we need to call va_end after the last variable parameter is completed.

Limitations of variable parameters

Note that variable parameters must be visited from beginning to end. It is possible to assume that you want to end up halfway through a few variable parameters, but suppose you want to start by visiting the middle of the list of references, that's not going to work.

1. There is at least one named parameter in the list of references. If you don't have a named parameter, you can't use Va_start.

2. These macros are not directly inferred from the actual number of parameters that exist.

3. These macros cannot infer the type of each parameter.

4. Assuming that the wrong type is specified in the va_arg, the consequences are not pre-measured.

Variable List of parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.