Variable Length Parameter List va_arg

Source: Internet
Author: User

In actual programming, sometimes the function does not know how many parameters need to be passed before it can be determined during program execution. To a certain extent, it also hurts programmers. In fact, in C/C ++, a macro va_arg () that can be used to pass the variable length parameter list has been defined ().

First, let's look at an example, and then analyze the content and details related to the va_arg macro.

#include <IOSTREAM>#include <STRING>#include <CSTDIO>#include <CSTDARG>using namespace std;int gSum( int,...);int main( int argc, char*argv[]){int ans = gSum(4,5,6,7,1);cout<<"gSum(4,5,6,7,1)="<<ans<<endl;cin>>ans;return 0;}int gSum( int num,...){int ans=0;va_list argptr;va_start(argptr,num);for (; num>0; num--){ans +=va_arg(argptr,int);}va_end(argptr);return ans;}

The va_arg macro is in the cstdarg library. Therefore, you must include this library before the program starts. The execution result of the program is 5 + 6 + 7 + 1 = 19 as follows:

Through the execution result of the above program, we can see that we have implemented the function Sum of variable length parameters, in this type of program:

1) first, you must call va_start () to pass a valid parameter list va_list and the first parameter forced by the function. The first parameter indicates the number of parameters to be passed.

2) second, call va_arg () to pass the parameter list va_list and the type of the parameter to be returned. The return value of va_arg () is the current parameter.

3) Call va_arg () repeatedly for all parameters ()

4) Finally, it is necessary to call va_end () to pass the va_list pair to clear after completion.


Let's take a look at the prototypes of the three functions va_start (), va_arg (), and va_end:

1) type va_arg (va_list argptr, type );

2) void va_end (va_list argptr );

3) void va_start (va_list argptr, last_parm );

In fact, you can see from the source file that the above three functions are only some macros defined in C:

# Define va_start (AP, V) (AP = (va_list) & V + _ intsizeof (v ))

# Define va_arg (AP, t) (* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t )))
# Define va_end (AP) (AP = (va_list) 0)

The variable parameter list has many applications in actual programming.

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.