C-language function variable parameter list

Source: Internet
Author: User

Original: C-language function variable parameter list

In a function prototype, the parameters that the function expects to accept are listed, but the prototype 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, but there are some limitations.
Consider a function that calculates the average of a series of values. If these values are stored in an array, the task is too simple, so in order to make the problem more interesting, we assume that they are not stored in the array. Let's take a look at a poorly-calculated and less stable solution:

1 //calculates the average of a specified number of values (bad scheme)
2 floatAverageintN_value,intV1,intV2,intV3,intV4,intV5)
3{
4 floatSUM=V1;
5 if(n_values>=2)
6Sum+=v2;
7 if(n_values>=3)
8Sum+=v3;
9 if(n_values>=4)
TenSUM+=V4;
One if(n_values>=5)
ASUM+=V5;
- returnSum/n_values;
-}

There are a few problems with this function: first, it does not test the number of arguments, and it cannot detect this situation where there are 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.

STDARG macro
mutable parameter lists are implemented by macros that define the Stdarg.h header file, which is part of the standard library. This header file declares a type va_list and
Three macro--va_start, Va_arg, Va_end. We can declare a variable of type va_list, which is used in conjunction with these macros, to access the value of the parameter.
The following program uses three macros to correctly complete a task that calculates the average of a specified number of values.

1 //average of a specified number of values
2#include <stdarg.h>
3 float(intValues,...)
4{
5Va_list Var_arg;
6 intCount
7 floatsum=0;
8
9Var_start (var_arg,n_values);//preparing to access mutable parameters
Ten for(count=0; count<n_values;count+=1)//add a value from a mutable parameter table
One{
ASum+=var_varg (Var_arg,int);
-}
-Var_end (VAR_ARG);//complete processing of mutable parameters
the returnSum/n_values;
-}

C-language function 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.