Variable number of parameters
-
How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example
function(5,1,2,3,4,5)
orfunction(3,1,2,3)
Thanks -
How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example
function(5,1,2,3,4,5)
orfunction(3,1,2,3)
Thanksfor variable no. of arguments use va_arg, look into MSDN for more details
-
How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example
function(5,1,2,3,4,5)
orfunction(3,1,2,3)
Thanksreturn_type fonction (...);
the '...' is the C style to say that th function can have a variable number of parameters. if you want fixed parameters, with a variable list below , do this (
printf()
function for example):int printf(const char*, ...);
To access such parameters, use the macros
va_start
(),va_arg
() andva_end
(), plus theva_list
type.
TOXCCT >>> GEII power
-
How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example
function(5,1,2,3,4,5)
orfunction(3,1,2,3)
ThanksIn this particular case:
int function( int nParams, ... );
and then use
va_list
:int function( int nParams, ... )
{
int argValue;va_list args;
va_start( args, nParams );for ( int nParam = 0; nParam < nParams; ++nParam )
{
argValue = va_arg( args, int );// Do something with argValue
}
}Stability. What an interesting concept. -- Chris Maunder