how to use va_list

S

shiva

hello,

just i want to write a program which can take unknown number of
integers and sum these numbers using va_list,va_arg...

but, i am not able to recognize the last element in series.can u help
in this regard.

i want a generalized program , i dont want to terminate my series with
0 or any other pre-determined number.

u just tell me how can i recognize the last element.
i hv seen at the va_list example given in Turbo C help,but they r using
0 for end of numbers.
 
C

Chris Croughton

just i want to write a program which can take unknown number of
integers and sum these numbers using va_list,va_arg...

but, i am not able to recognize the last element in series.can u help
in this regard.

No, 'u' is not a defined function or parameter. (Hint: write in normal
English, not some off abbreviated version.)
i want a generalized program , i dont want to terminate my series with
0 or any other pre-determined number.

You'll need to pass the number of numbers in as the first parameter,
then.
u just tell me how can i recognize the last element.

Unless it is a special value, you (not 'u') can't.
i hv seen at the va_list example given in Turbo C help,but they r using
0 for end of numbers.

That's one way to you it. Or you have to pass in the number of
parameters another way (for instance printf() does it by looking at the
format string), but you then have problems if you pass fewer parameters
than you say you have.

Chris C
 
J

Jens.Toerring

shiva said:
just i want to write a program which can take unknown number of
integers and sum these numbers using va_list,va_arg...

I guess you mean a function, not program, since the va_xxx() macros
only make sense in function context...
but, i am not able to recognize the last element in series.can u help
in this regard.
i want a generalized program , i dont want to terminate my series with
0 or any other pre-determined number.
u just tell me how can i recognize the last element.
i hv seen at the va_list example given in Turbo C help,but they r using
0 for end of numbers.

Since a function with a variable number of arguments needs at least one
named argument (otherwise you can't use the va_start() macro and thus
you would be unable to access the unnamed arguments at all) there is
really no problem, just make the number of integers you pass to your
function the (last) named argument. So your function might look like
this:

int add_them_up( int arg_count, ... )
{
va_list ap;
int sum = 0;

va_start( ap, arg_count );
for ( ; arg_count > 0; arg_count-- )
sum += va_arg( ap, int );
va_end( ap );
return sum;
}

There's really no other way to detect the end of the list of unnamed
arguments when you can't use a special value to indicate the end of
the list.
Regards, Jens
 
C

Christian Bau

"shiva said:
hello,

just i want to write a program which can take unknown number of
integers and sum these numbers using va_list,va_arg...

but, i am not able to recognize the last element in series.can u help
in this regard.

i want a generalized program , i dont want to terminate my series with
0 or any other pre-determined number.


There is no way for a function with variable number of arguments to find
out how many values you are passing to it. You either have to terminate
it with a predefined value, or pass the number of values as the first
argument, or use a method like printf, where the format string
determines how many and what kind of values are following.
u just tell me how can i recognize the last element.
i hv seen at the va_list example given in Turbo C help,but they r using
0 for end of numbers.

I have been told before by posters from India, that using weird english
like "u", "i hv", is supposed to sound clever. Unfortunately, that is
exactly the kind of language that a clueless thirteen year old English
or American child would use to sound clever, and many readers will
correctly or incorrectly assume that you are just as clever as a
clueless thirteen year old English or American child.

Try to use "you", "are", "have", and please write "I" with an uppercase
letter. English is case sensitive, just like C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top