Variable parameters in C ++ and Java

Source: Internet
Author: User

● Test code:

The sample code on msdn is really bad to tell the truth. It does not explain the features of several important Macros in Variable Parameter implementation.

 

Code
# Include "stdafx. H"
# Include <stdio. h>
# Include <stdarg. h> // This header file must be referenced when variable parameters are used.

Int avarage (INT ncount ,)
/*
The parameter cannot completely use "". There must be at least one parameter. In essence, for method implementation, you need to know the variable parameter quantity before extracting the variable parameter; otherwise, you cannot know the pointer range.
*/

{
Va_list Params; // defines va_list, which is used to access input real parameters.
Va_start (Params, ncount); // initialize va_list, which must be specified as the first parameter of the variable parameter.

Int ntotal = 0;
Int nbuffer = 0;

For (INT Index = 0; index <ncount; index ++)
{
Nbuffer = va_arg (Params, INT); // obtain the parameter and move the pointer to the next position.
Ntotal + = nbuffer;
}

Return ntotal? (Ntotal/ncount): 0;
}

Int main (INT argc, char * argv [])
{
Printf ("% d", avarage (2, 10, 20 ));
Return 1;
}

Running result:
15.

 

● Note:

 

The variable parameters in C language are actually passed through
Va_list
Va_arg
Va_end macro.

The va_list macro can obtain the first parameter in the variable parameter. (You need to specify the first parameter of the Variable Parameter)
Va_arg gets a parameter in the Variable Parameter and moves the pointer down to the next parameter location.

Because of the implementation method of C, the value range of va_arg cannot be determined when the Variable Parameter Function is implemented.
Therefore, you must specify the actual number of variable parameters before parsing the variable parameters.

For example:

Printf (char * format ,...);
It seems that the number of variable parameters is not passed in, but the format strings in the format actually show the number and type of variable parameters.
For example, "% d, % s, % s" indicates a total of three parameters. The first parameter is an integer, and the last two parameters are strings.

In Java, it seems easier to change the parameter method.

For example, print (string... ARGs)
In the method, argS is actually a string [].
You can use for each to easily traverse variable parameters.
You can also use args. Length () to obtain the actual number of variable parameters.

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.