Averaging with a variable parameter list

Source: Internet
Author: User

<stdarg.h>//Header files

va_list arg access parameter list not determined part

Va_start (Arg,val);/ /Set arg_ptr to the first optional argument passed to the function argument list arg is the variable name declared, Val is the last parameter of the determined argument list

Va_arg (arg,int);//fromarg_ptrthe given location searchtypevalue, and thearg_ptrpoint to the next parameter in the list, using thetypethe range determines where the next parameter starts。 int is the type of the next parameter

Va_end (ARG);// reset pointer to NULL

#include <stdio.h> #include <stdarg.h>double aver (int val, ...) {va_list Arg;int i = 0;double sum = 0.0;va_start (ARG, Val); for (i = 0; i < val; i++) {sum = sum + va_arg (arg, int);} Va_end (ARG); return sum/val;} int main () {double ret = aver (4,3,6,4,9);p rintf ("%f", ret); return 0;}

Limitations of the mutable parameter list:

(1) All mutable parameters must be accessed and cannot be accessed only by part

(2) There is at least one named parameter that cannot be used Va_start

(3) cannot specify error parameter type in Va_arg


Averaging with a 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.