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;
}