473,785 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9458
shiva <du*********@gm ail.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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
On 21 Feb 2005 01:09:48 -0800, shiva
<du*********@gm ail.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************ *********@o13g2 000cwo.googlegr oups.com>,
"shiva" <du*********@gm ail.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
3693
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 parameter preceding first optional argument? In another words, I want to create a va_list in a similar manner as creating an array. The reason is I want to use FormatMessage() in my program where it
3
16782
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 which uses the "..." as the last parameter how can I pass them to the orignal printf ? void myprintf(char *txt, ...) printf(txt, ???????); }
2
3345
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 platform (AS/400) where it fails: The 'va_list' is actually defined as some some kind of an array, so passing it happens by *reference*: The first user will destroy it and the second
2
2540
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
3075
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 when I call it directly. Now I have a second function (set) that also calls add. But this doesn't
7
2863
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 parameters off arg */ }
1
9476
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 something like %V to pass in another format string and a va_list to allow nesting. It happens to work on my compiler, but I wasn't sure if it's portable to use va_list's as parameters to a variable argument function because va_list isn't always just a...
5
4668
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 holding information needed by the macros
6
4065
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 inlined because it uses variable argument lists variadic.c: In function ‘vf’: variadic.c:12: warning: inlining failed in call to ‘fv’: function not inlinable variadic.c:27: warning: called from here
1
5549
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 editions Build tools: Visual Studio 2008. SLES 10 SP1, both 32-bit & 64 bit editions
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.