Three macros stdarg. h: va_start, va_arg, and va_end

Source: Internet
Author: User

The C function must beProgramThe following macros are used:
Void va_start (va_list arg_ptr, prev_param );

Type va_arg (va_list arg_ptr, type );

Void va_end (va_list arg_ptr );

Va here is the meaning of Variable-argument (Variable Parameter.
These macros are defined in stdarg. H, so programs that use variable parameters should include
Header file. Below we will write a simple variable parameter function. The function should have at least one integer.
Parameter. The second parameter is also an integer and is optional. The function only prints the values of these two parameters.

Void simple_va_fun (int I ,...)
{
Va_list arg_ptr;
Int J = 0;

Va_start (arg_ptr, I );
J = va_arg (arg_ptr, INT );
Va_end (arg_ptr );
Printf ("% d/N", I, j );
Return;
}

We can declare our function in our header file as follows:
Extern void simple_va_fun (int I ,...);
We can call it in the program as follows:
Simple_va_fun (100 );
Simple_va_fun (100,200 );

From the implementation of this function, we can see that the following steps should be taken to use variable parameters:
1) first define a va_list variable in the function. Here is arg_ptr. This variable
Volume is a pointer to a parameter.
2) then use the va_start macro to initialize the variable arg_ptr. The second parameter of this macro is
The previous parameter of a variable parameter is a fixed parameter.
3) then return the variable parameter with va_arg and assign the value to the second integer J. va_arg.
The parameter is the type of the parameter you want to return. Here it is the int type.
4) use the va_end macro to end variable parameter acquisition. Then you can make
The second parameter is used. If the function has multiple variable parameters, call va_arg to obtain
Take parameters.

Http://blog.csdn.net/zxianrong/article/details/3706405

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.