Variable Parameter List in C

Source: Internet
Author: User

I just want to record the usage of the Variable Parameter List in C. I can't remember it later and have no reference manual at hand. For convenience, I wrote it in C ++, don't blame me.

//
// Variable Parameter List Example
//

# Include <cstdlib>
# Include <cstdarg>
# Include <climits>
# Include <iostream>

Int max (INT num ,...)
{
Int M = int_min;

//
// Parameter pointer AP (argument pointer ).
// Traverses the variable parameter list.
//
Va_list AP;

//
// Initialize the parameter pointer AP.
// Set the AP as the first variable parameter passed to the function.
// Va_start must be called before any va_arg or va_end call.
//
Va_start (AP, num );

For (INT I = 0, T; I <num; I ++)
{
//
// Return the value of the next parameter in the parameter list.
// Push the AP pointer to the next parameter (if any ).
// After va_start is called, The first variable parameter value will be returned if va_arg is called for the first time.
//
T = va_arg (AP, INT );

If (T> m ){
M = T;
}
}

//
// Perform necessary cleanup on the AP and va_list.
// Va_end is called after all parameters are read through va_arg.
//
Va_end (AP );

Return m;
}

Int main (void)
{
STD: cout <max (5, 5, 6, 3, 8, 5) <STD: Endl;

Return exit_success;
}

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.