Variable parameter function of C language

Source: Internet
Author: User

Stdarg.h

Stdarg.h is the header file of the C standard function library, and the main purpose is to allow the function to receive indeterminate parameters. When you need to define a variadic function, you need to include the header file.




Let's start with an example, the idea from <

#include <stdio.h> #include <stdarg.h>enum drink{apple = 3,banana = 4,orange = 5,other = 6};int count (int n,.. .) {    int total = 0;        Va_list ap;    Va_start (ap,n);        for (int i = 0; i < n; ++i) {Total        + = (int) Va_arg (AP, enum drink);    }    Va_end (AP);    return total;} int main () {    printf ("Cost%d\n", Count (3,apple,orange,other));    printf ("Cost%d\n", Count (5,apple,apple,banana,other,orange));}


This example defines the type of beverage and sets the price of the beverage.

After that, a function count is defined to tally the price, the first parameter indicates the number of purchases, then the purchased symbol (variable), and finally the total price to be spent.

The indeterminate parameter function has at least one named parameter


Output:


Cost 14

Cost 21

Program ended with exit code:0





Now say the principle:

Now that the function has at least one named parameter, you have a datum that defines the pointer to the starting address of the subsequent parameter based on the address of the datum variable.

If the type of the subsequent parameter is known, the value of the parameter can be obtained by pointer, depending on the length of the parameter type.




Let's introduce the use of those 4 macros:


Va_list: Define the pointer above

Va_start: Locates the address of the subsequent variable. Since there is at least one variable parameter (can have more than one), the starting address of the subsequent parameter is known based on the last known parameter being positioned.

Va_arg: The data is taken out by pointer based on the type of subsequent parameters.

Va_end: Do some finishing work




Finally come back to explain the previous example:

First, a pointer va_list ap is defined;

The pointer ap is connected to the known parameter n by the Va_start function to locate the starting address of the subsequent parameter.

After positioning, pass the Va_arg function. Use the pointer ap and parameter types to get the data, because the first parameter of the example is the number of times that the for loop is to be summed.

Finally ended Va_end.





Report:

Variable parameter Learning Notes







Variable parameter function of C language

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.