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. 10 2661
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. 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
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
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
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
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
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
"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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kwan Ting |
last post by:
The_Sage, I see you've gotten yourself a twin asking for program in
comp.lang.c++ .
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1
If you the oh so mighty...
|
by: Stig Brautaset |
last post by:
Hi group,
I'm playing with a little generic linked list/stack library, and have a
little problem with the interface of the pop() function. If I used a
struct like this it would be simple:
...
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: sunglo |
last post by:
My doubt comes from trying to understand how thread return values work
(I know, it's off topic here), and I'm wondering about the meaning of
the "void **" parameter that pthread_join expects (I...
|
by: Juggernaut |
last post by:
I am trying to create a p_thread
pthread_create(&threads, &attr, Teste, (void *)var);
where var is a char variable.
But this doesnt't work, I get this message:
test.c:58: warning: cast to pointer...
|
by: Stijn van Dongen |
last post by:
A question about void*. I have a hash library where the hash create
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function...
|
by: maadhuu |
last post by:
hello, this is a piece of code ,which is giving an error.
#include<stdio.h>
int main()
{
int a =10;
void *p = &a;
printf("%d ", *p ); //error....why should it //be an error ?can't the...
|
by: Erik de Castro Lopo |
last post by:
Hi all,
The GNU C compiler allows a void pointer to be incremented and
the behaviour is equivalent to incrementing a char pointer.
Is this legal C99 or is this a GNU C extention?
Thanks in...
|
by: Giannis Papadopoulos |
last post by:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be...
|
by: brianhray |
last post by:
Hello:
I am writing a C interface and was curious how/why a void* can be used
as a reference parameter.
////////////////////////////////////////////////////////////
// WORKS:
// Client1.h...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |