Variable parameter list

Source: Internet
Author: User

In a function prototype, the parameters that the function expects to accept are listed, but the function can only display a fixed number of arguments. Is it possible for a function to accept a different number of arguments at different times? The answer is yes (printf is a variable parameter 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, there are a few problems with this function: first, it does not test the number of parameters, and it cannot detect this situation with too many parameters. But this problem is easy to solve, simply add the test. Second, a function cannot handle more than 5 values. To solve this problem, you only have to add some similar code to the already bloated code. There is also a more serious problem: the correspondence between the real parameter and the formal parameter 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 a mutable parameter list is introduced in C language.

STDARG macro

mutable parameter lists are implemented by macros that are defined in the Stdarg.h header file, which is part of the standard library.

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 access the variable parameter ten     for (count=0;count<n_values;count+=1)//Add a value from the variable parameter table Each     {        sum+=var_varg (var_arg,int);     }14     var_end (var_arg);       Complete processing of variable parameters 15     


There are several 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 a variable parameter, which is actually a pointer.

va_start (): Span style= "font-family: Arial" is a VL points to the first mutable parameter, which is called after the 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 argument list.

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

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

Finally, after the last mutable parameter is accessed, we need to call va_end.

Limitations of variable parameters

Note that mutable parameters must be accessed one by one. This is OK if you want to end up halfway after accessing several mutable parameters, but if you want to access the parameters in the middle of the parameter list at the beginning, that is not possible.

1. There is at least one named parameter in the parameter list. If you do not have a named parameter, you cannot use Va_start.

2. These macros are not directly able to determine the number of actual parameters that exist.

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

4. If the wrong type is specified in the va_arg, the consequence is unpredictable.

Variable parameter list

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.