473,385 Members | 1,912 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,385 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 9407
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.