The "C language" variable parameter list.

Source: Internet
Author: User

(1) First look at a function of averaging
#include <stdio.h>DoubleAverageintValintV1,intV2,intV3,intV4,intV5) {Double sum= V1;if(Val >=2)sum+ = v2;if(Val >=3)sum+ = V3;if(Val >=4)sum+ = V4;if(Val >=5)sum+ = v5;return sum/val;}intMain () {printf ("%f\n", average (5,1,3,5,6,7));return 0;}
·This function is cumbersome when we want to ask for an average of many values. ·In C + +, the number of arguments in an argument can be inconsistent with the number of formal parameters ·In C, the number of arguments in the argument must be the same as the number of formal parameters, or the following error occurs

(2) In order to solve the above problem, C language provides us with a 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. What if you have a function that accepts a different number of arguments at different times? STDARG macro ·Variable parameter lists can be implemented by macros defined in the Stdarg.h header file, which is part of the standard library. This header file declares a type va_list and a 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.
#include <stdio.h>#include <stdarg.h>DoubleAverageintVal, ...) {inti =0; Va_list Arg;Double sum=0.0; Va_start (Arg, Val); for(i =0; I < Val; i++) {sum+ = Va_arg (ARG,int); } va_end (ARG);return sum/Val;}intMain () {Doubleret = AVERAGE (6,1,2,3,4,5,8); printf"%f\n", ret);return 0;}
a) the ellipsis in the above function, which indicates that an indeterminate number and type of parameters may be passed here. b) The function declares a variable called Var_arg, which is used to access an indeterminate part of the parameter list. This variable is initialized by calling Va_start. Its 1th parameter is the name of the va_list variable, and the last named argument before the 2nd parameter ellipsis. The initialization process sets the VAR_ARG variable to the 1th parameter that points to the variable parameter section. c) To access the parameters, you need to use VA_ARG, which receives two parameters: the va_list variable and the type of the next parameter in the argument list. In this example, all mutable parameters are shaped, and in some functions you may want to determine the type of the next parameter by the data you obtained earlier. Va_arg returns the value of this parameter and causes Var_arg to point to the next mutable parameter. d) when you use the STDARG macro definition, you must have at least one named parameter in the parameter list.

The "C language" 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.