Full solution to variable parameters in C Language

Source: Internet
Author: User

Cstdarg (stdarg. h)
Variable Parameter Processing
This header file defines some macros that can be used to access the list of parameters without names of the called functions one by one. The number of these parameters
And type are unknown.
A function can receive variable numbers of additional parameters by including a comma and three vertices (,...) without providing corresponding parameter definitions.
These variable parameters must appear after the common named parameters.

Return Value Function Name (parameter declaration ,...);
You can use the macros va_start, va_arg, and va_end defined in this header file to access additional parameters.
* First, va_start initializes a variable parameter list named va_list;
* The execution of the series va_arg will generate additional parameter values according to the order in which the parameters are passed to the function;
* The most Hu, va_end must be executed before the function returns.

This header file contains a type:
The type of va_list that contains variable parameter information. (Type)
Three functions:
Va_start initializes a variable parameter list. (Macro)
Va_arg gets the next parameter. (Macro)
Va_end stops using the variable parameter list. (Macro)

Some C compiler implementations (such as ISO C99 compatible compilers) also contain a va_copy macro to copy a va_list object,
However, this is not part of the iso c ++ standard.


Va_start
Void va_start (va_list ap, paramN );
Initialize Variable Parameter List
Initialize the most passed-In va_list object ap. The ap saves the variable parameter after obtaining the parameter paramN through va_arg.
Information.
If a function executes va_start, it must execute va_end before the function returns.

Parameters
Ap
An object of the va_list type stores information about variable parameters obtained through va_arg.
ParamN
The name of the last name parameter of the function definition.

Return Value
None


Example

[Cpp] <span style = "font-size: 16px;">/* va_start example */
# Include <stdio. h>
# Include <stdarg. h>
 
Void PrintFloats (int amount ,...)

 


{
Int I;
Double val;
Printf ("Floats passed :");
Va_list vl;
Va_start (vl, amount );
For (I = 0; I <amount; I ++)
{
Val = va_arg (vl, double );
Printf ("\ t %. 2f", val );
}
Va_end (vl );
Printf ("\ n ");
}
 
Int main ()
{
PrintFloats (3, 3.14159, 2.71828, 1.41421 );
Return 0;
} </Span>
<Span style = "font-size: 16px;">/* va_start example */
# Include <stdio. h>
# Include <stdarg. h>

Void PrintFloats (int amount ,...)
{
Int I;
Double val;
Printf ("Floats passed :");
Va_list vl;
Va_start (vl, amount );
For (I = 0; I <amount; I ++)
{
Val = va_arg (vl, double );
Printf ("\ t %. 2f", val );
}
Va_end (vl );
Printf ("\ n ");
}

Int main ()
{
PrintFloats (3, 3.14159, 2.71828, 1.41421 );
Return 0;
} </Span>
This function PrintFloats uses amount as its first parameter, representing the number of additional parameters, which will be obtained through the macro read defined by cst1_g,
And printed in a special format.


Va_arg
Type va_arg (va_list ap, type)
Get the next Parameter
This macro is expanded into an expression that contains the type and value of the next parameter in the Variable Parameter List.
The next macro call will show the next parameter passed to the function in the same order.
Note that va_arg cannot determine the actual type of the parameter passed to the function, but the macro type can be used as its type.
Also, note that va_arg cannot determine whether the obtained parameter is the last one passed to the function. The function must be as follows:
DESIGN: the number of parameters must be determined by the read-name or unnamed parameter values.

Parameter www.2cto.com
Ap
A va_list object. Before calling va_arg, this parameter must be initialized by calling va_start.
Type
A type name. This type name is the type of the expression after the macro is expanded (for example, its return type ). A legal fit for va_arg
The type expression used must be as follows: When a * sign is added to its right side, its result expression must be a valid type
Pointer.

Return Value
Return the next additional parameter using the type expression.

Example


[Cpp] <span style = "font-size: 16px;">/* va_arg example */
# Include <stdio. h>
# Include <stdarg. h>
 
 
Int FindMax (int amount ,...)
{
Int I, val, greater;
Va_list vl;
Va_start (vl, amount );
Greater = va_arg (vl, int );
For (I = 1; I <amount; I ++)
{
Val = va_arg (vl, int );
Greater = (greater> val )? Greater: val;
}
Va_end (vl );
Return greater;
}
 
 
Int main ()
{
Int m;
M = FindMax (7,702,422,631,834,892,104,772 );
Printf ("The greatest one is: % d \ n", m );
Return 0;
} </Span>
<Span style = "font-size: 16px;">/* va_arg example */
# Include <stdio. h>
# Include <stdarg. h>


Int FindMax (int amount ,...)
{
Int I, val, greater;
Va_list vl;
Va_start (vl, amount );
Greater = va_arg (vl, int );
For (I = 1; I <amount; I ++)
{
Val = va_arg (vl, int );
Greater = (greater> val )? Greater: val;
}
Va_end (vl );
Return greater;
}


Int main ()
{
Int m;
M = FindMax (7,702,422,631,834,892,104,772 );
Printf ("The greatest one is: % d \ n", m );
Return 0;
} </Span>


The FindMax function uses its first parameter to save the number of additional parameters that will be obtained. The first additional parameter is obtained and used as the initial
And then obtain the remaining parameters in a loop and return the largest one (892 here ).


Va_end
Void va_end (va_list ap );
Execute the appropriate action so that we can get the normal response from the function that uses the ap of the va_list object to obtain additional parameters.
Regardless of when va_start is executed, this macro must be executed before the function is returned.
Parameters
Ap
Va_list object, which is the object previously initialized through va_start.
Return Value
None

Example


[Cpp] <span style = "font-size: 16px;">/* va_arg example */
# Include <stdio. h>
# Include <stdarg. h>
 
Void PrintLines (char * first ,...)
{
Char * str;
Va_list vl;
 
Str = first;
 
Va_start (vl, first );
 
Do {
Printf ("% s \ n", str );
Str = va_arg (vl, char *);
} While (str! = NULL );
 
Va_end (vl );
}
 
Int main ()
{
PrintLines ("First", "Second", "Third", "Fourth", NULL );
Return 0;
} </Span>
<Span style = "font-size: 16px;">/* va_arg example */
# Include <stdio. h>
# Include <stdarg. h>

Void PrintLines (char * first ,...)
{
Char * str;
Va_list vl;

Str = first;

Va_start (vl, first );

Do {
Printf ("% s \ n", str );
Str = va_arg (vl, char *);
} While (str! = NULL );

Va_end (vl );
}

Int main ()
{
PrintLines ("First", "Second", "Third", "Fourth", NULL );
Return 0;
} </Span>
The PrintLines function uses variable parameters. The first parameter is passed first, but the remaining parameters are all used in the do-while loop.
Once obtained by va_arg, the loop ends when the variable parameter obtained is a null pointer.

 

From the column chenlong12580

Related Article

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.