Function with variable number of arguments
-
Hi! Is there any way I can call a function taking a variable number of arguments without specifying a terminating value? For example, I have this function:
int sum( int a, ... );
that calculates the sum of a variable number of positive arguments and takes as a final argument the value -1. If I want to get 1 + 2 + 3, I would do something like this:sum( 1, 2, 3, -1 );
What I would like to do is call this function without having to specify the fourth argument, -1, which tells the function to stop parsing the variable number of arguments and adding them. Is there any possible way to accomplish this? Thanks in advance! -
Hi! Is there any way I can call a function taking a variable number of arguments without specifying a terminating value? For example, I have this function:
int sum( int a, ... );
that calculates the sum of a variable number of positive arguments and takes as a final argument the value -1. If I want to get 1 + 2 + 3, I would do something like this:sum( 1, 2, 3, -1 );
What I would like to do is call this function without having to specify the fourth argument, -1, which tells the function to stop parsing the variable number of arguments and adding them. Is there any possible way to accomplish this? Thanks in advance!void va_start( va_list arg_ptr, prev_param //Parameter preceding first optional argument (ANSI only). ); // (ANSI version)
My blogs: http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng http://bloglines.com/public/jiangsheng Command what is yours Conquer what is not ---Kane -
void va_start( va_list arg_ptr, prev_param //Parameter preceding first optional argument (ANSI only). ); // (ANSI version)
My blogs: http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng http://bloglines.com/public/jiangsheng Command what is yours Conquer what is not ---KaneI know I have to use the va_start, va_arg and va_end macros defined in stdarg.h in order to manipulate the arguments of such a function. But what I don't know is how I can call that function without a terminating value, like this:
sum( 1, 2, 3 );
Instead of:sum( 1, 2, 3, -1 );
I want to make the function sum( ) "know" when to stop adding its arguments without giving it a terminating negative value argument. Thanks! -
I know I have to use the va_start, va_arg and va_end macros defined in stdarg.h in order to manipulate the arguments of such a function. But what I don't know is how I can call that function without a terminating value, like this:
sum( 1, 2, 3 );
Instead of:sum( 1, 2, 3, -1 );
I want to make the function sum( ) "know" when to stop adding its arguments without giving it a terminating negative value argument. Thanks!No, you have to supply some sort of method identifying the number of parameters. va_arg() is a macro that simply advances the pointer to your parameters, it has no way of knowing if the data it is pointing at is valid or not.
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!