473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

how to use va_list

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.

Nov 14 '05 #1
3 9401
shiva <du*********@gmail.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
On 21 Feb 2005 01:09:48 -0800, shiva
<du*********@gmail.com> wrote:
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
Nov 14 '05 #3
In article <11*********************@o13g2000cwo.googlegroups. com>,
"shiva" <du*********@gmail.com> wrote:
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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter | last post by:
-------------------------------------------------------------------------------- Hi, I was wondering if we can create a va_list by adding objects in the va_list instead of passing in the...
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
2
by: Joerg Schoen | last post by:
Hi folks! I have a function that gets a 'va_list'. I am passing the 'va_list' two times to a function like 'vprintf' to print it out. I thought that this was portable until I came across a...
2
by: j0mbolar | last post by:
given this example: void bar(const char *fmt, ...) { va_list ap; va_start(ap, fmt); baz(fmt, ap); va_end(ap);
11
by: thierrydollar | last post by:
Hi, I have written a very simple program using variable arguments calls and I get strange things that I cannot explain. I have one function (add) that displays two parameters. It works well...
7
by: Flash Gordon | last post by:
Reading the standard, va_list is an object type (, so I believe the following should be possible: #include <stdarg.h> void foo(va_list *arg) { /* do some stuff which conditionally might read...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
5
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for...
6
by: Laurent Deniau | last post by:
When I compile the code below with gcc -std=c99 -W -Wall -pedantic -O3 -Winline, it reports the following: variadic.c: In function ‘fv’: variadic.c:12: warning: function ‘fv’ can never be...
1
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.