By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

void fn(...)

P: n/a
Hi,
I'm quite new to C++ and I found the other day something I didn't
understand. One of the arguments of a function was "..." (without the
quotes). What does it mean?
Thanks for your help.

Jun 26 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
In article <11**********************@i40g2000cwc.googlegroups .com>,
ge*******@gmail.com says...
Hi,
I'm quite new to C++ and I found the other day something I didn't
understand. One of the arguments of a function was "..." (without the
quotes). What does it mean?


It means that's a function (like printf) that accepts a variable
number of arguments.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 26 '06 #2

P: n/a
ge*******@gmail.com wrote:
I'm quite new to C++ and I found the other day something I didn't
understand. One of the arguments of a function was "..." (without the
quotes). What does it mean?


Variable number/type of arguments. Read your favourite C++ book about
them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 26 '06 #3

P: n/a
Victor Bazarov wrote:
ge*******@gmail.com wrote:
I'm quite new to C++ and I found the other day something I didn't
understand. One of the arguments of a function was "..." (without the
quotes). What does it mean?


Variable number/type of arguments. Read your favourite C++ book about
them.

V


One more point to add:

It is important to know this stuff, yet equally important not to use or
use it with great caution.

Regards,
Ben
Jun 27 '06 #4

P: n/a
Have a look at those functions for accessing variable-argument lists.

va_arg, va_start, va_end

Here is a sample:

#include <stdio.h>
#include <stdarg.h>
int average( int first, ... );

void main( void )
{
/* Call with 3 integers (-1 is used as terminator). */
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

/* Call with 4 integers. */
printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

/* Call with just -1 terminator. */
printf( "Average is: %d\n", average( -1 ) );
}

/* Returns the average of a variable list of integers. */
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}

The outputs are:

Average is: 3
Average is: 8
Average is: 0

Regards,

Yong Hu

benben wrote:
Victor Bazarov wrote:
ge*******@gmail.com wrote:
I'm quite new to C++ and I found the other day something I didn't
understand. One of the arguments of a function was "..." (without the
quotes). What does it mean?


Variable number/type of arguments. Read your favourite C++ book about
them.

V


One more point to add:

It is important to know this stuff, yet equally important not to use or
use it with great caution.

Regards,
Ben


Jun 27 '06 #5

P: n/a

Yong Hu 写道:
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
Is it possible to write the function without flag -1 and also without
other tricks to tell the number of parameters?

va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}

The outputs are:

Average is: 3
Average is: 8
Average is: 0

Regards,

Yong Hu


Jun 27 '06 #6

P: n/a

cdrsir wrote:
Yong Hu 写道:
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
Is it possible to write the function without flag -1 and also without
other tricks to tell the number of parameters?


If the flag -1 was not used, the number of parameters has to be given:

int average( int num, int first, ... )
{
int sum, i;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */

sum = first;

for ( i =1; i<num; i++ )
{
sum += va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}

va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}

The outputs are:

Average is: 3
Average is: 8
Average is: 0

Regards,

Yong Hu


Jun 27 '06 #7

P: n/a
Yong Hu wrote:
cdrsir wrote:
Yong Hu ??:
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments.
*/ while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}


Is it possible to write the function without flag -1 and also without
other tricks to tell the number of parameters?


If the flag -1 was not used, the number of parameters has to be given:

int average( int num, int first, ... )
{
int sum, i;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */

sum = first;

for ( i =1; i<num; i++ )
{
sum += va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}


You actually don't need the 'first' since you have 'num'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '06 #8

P: n/a

"cdrsir" <cd****@gmail.com> wrote in message
Is it possible to write the function without flag -1 and also without

other tricks to tell the number of parameters?

No. You must create some sort of home-brewed mechanism to determine how
many parameters to process. In this simple case, a sentinel argument of -1
was used. In a little bit more elaborate case such as printf( ), the
format string determines the parameters to read.

Regardless of how you do it, you have to come up with the scheme to know
when to stop reading the parameters.

Paul

Jun 27 '06 #9

P: n/a

Victor Bazarov wrote:
Yong Hu wrote:
cdrsir wrote:
Yong Hu ??:

int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments.
*/ while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}

Is it possible to write the function without flag -1 and also without
other tricks to tell the number of parameters?

If the flag -1 was not used, the number of parameters has to be given:

int average( int num, int first, ... )
{
int sum, i;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */

sum = first;

for ( i =1; i<num; i++ )
{
sum += va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}


You actually don't need the 'first' since you have 'num'.


Ok. But what will happen if you call average(2, 1.2, 1.3) instead of
average(2, 1, 1) ?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jun 28 '06 #10

P: n/a
Yong Hu wrote:
Victor Bazarov wrote:
Yong Hu wrote:
cdrsir wrote:
Yong Hu ??:

> int average( int first, ... )
> {
> int count = 0, sum = 0, i = first;
> va_list marker;
>
> va_start( marker, first ); /* Initialize variable
> arguments. */ while( i != -1 )
> {
> sum += i;
> count++;
> i = va_arg( marker, int);
> }

Is it possible to write the function without flag -1 and also
without other tricks to tell the number of parameters?
If the flag -1 was not used, the number of parameters has to be
given:

int average( int num, int first, ... )
{
int sum, i;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments.
*/

sum = first;

for ( i =1; i<num; i++ )
{
sum += va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments.
*/ return( sum ? (sum / count) : 0 );
}


You actually don't need the 'first' since you have 'num'.


Ok. But what will happen if you call average(2, 1.2, 1.3) instead of
average(2, 1, 1) ?


The behaviour is undefined. If you intend to interpret arguments as
'int's, passing 'double's there pulls all stops. The same thing would
happen in *your* variation if you call 'average(3, 1, 2, 3.0)' or
even 'average(2, 0, 0.0)'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 28 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.