473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with va_list etc

Can anyone point me to any good online references
about the va_arg() va_list() functions/macros

I googled and found a couple of things, but what I found, failed to explain
how it really works and give a easy to follow example program

Any pointers appreciated.


Nov 14 '05 #1
7 2309
Alex wrote:
Can anyone point me to any good online references
about the va_arg() va_list() functions/macros

I googled and found a couple of things, but what I found, failed to explain
how it really works and give a easy to follow example program

Any pointers appreciated.

#include <stdarg.h>

/* yadda yadda, main, and prototypes, and all that jazz */
int foo_varargs( char *fmt, ... ) {
va_list ap, bp;
int foo;

/* This starts the "va_*" functions up, the format
is your va_list, and the last argument defined before ... */
va_start( ap, fmt );
/* Now, you can get an arg from the list by type etc... It will
forward to the next argument, and va_arg will automagically cast
to "type", (in this case int), hence why you can pass
an integer where you should have passed a string where you
defined %s in the format string, and endup with a GPF, or
segv ;} */
foo = va_arg( &ap, int );
/* Or copy one list to another... */
va_copy( bp, ap );
/* And when you're done, do this */
va_end( ap );
/* Got it? */
return 0;
}
Most of the junk that is in a format string for such functions
is parsed inside the function too, so you get the idea.

-- The Dragon
Nov 14 '05 #2
Thanks for the reply!

How do you determine when you've reached the end of the arguement list?

say you parse 5 arguements to a function, but the code in the function is
processing
each arguement till it reaches the end, how could code be written to check
if the arguement
being read, is the last one?
"The Dragon" <no*****@nospam .com> wrote in message
news:nc******** ************@ro gers.com...
Alex wrote:
Can anyone point me to any good online references
about the va_arg() va_list() functions/macros

I googled and found a couple of things, but what I found, failed to explain how it really works and give a easy to follow example program

Any pointers appreciated.

#include <stdarg.h>

/* yadda yadda, main, and prototypes, and all that jazz */
int foo_varargs( char *fmt, ... ) {
va_list ap, bp;
int foo;

/* This starts the "va_*" functions up, the format
is your va_list, and the last argument defined before ... */
va_start( ap, fmt );
/* Now, you can get an arg from the list by type etc... It will
forward to the next argument, and va_arg will automagically cast
to "type", (in this case int), hence why you can pass
an integer where you should have passed a string where you
defined %s in the format string, and endup with a GPF, or
segv ;} */
foo = va_arg( &ap, int );
/* Or copy one list to another... */
va_copy( bp, ap );
/* And when you're done, do this */
va_end( ap );
/* Got it? */
return 0;
}
Most of the junk that is in a format string for such functions
is parsed inside the function too, so you get the idea.

-- The Dragon

Nov 14 '05 #3

"Alex" <ye*@yep.com> wrote in message
news:41******** *************** @news.optusnet. com.au...
Thanks for the reply!

How do you determine when you've reached the end of the arguement list?

say you parse 5 arguements to a function, but the code in the function is
processing
each arguement till it reaches the end, how could code be written to check
if the arguement
being read, is the last one?
Just test for the null string terminator in fmt. If all the data types will
be the same then you could just pass the number of arguments instead of a
format string.

Mike H.

"The Dragon" <no*****@nospam .com> wrote in message
news:nc******** ************@ro gers.com...
Alex wrote:
> Can anyone point me to any good online references
> about the va_arg() va_list() functions/macros
>
> I googled and found a couple of things, but what I found, failed to explain > how it really works and give a easy to follow example program
>
> Any pointers appreciated.
>
>
>
>

#include <stdarg.h>

/* yadda yadda, main, and prototypes, and all that jazz */
int foo_varargs( char *fmt, ... ) {
va_list ap, bp;
int foo;

/* This starts the "va_*" functions up, the format
is your va_list, and the last argument defined before ... */
va_start( ap, fmt );
/* Now, you can get an arg from the list by type etc... It will
forward to the next argument, and va_arg will automagically cast
to "type", (in this case int), hence why you can pass
an integer where you should have passed a string where you
defined %s in the format string, and endup with a GPF, or
segv ;} */
foo = va_arg( &ap, int );
/* Or copy one list to another... */
va_copy( bp, ap );
/* And when you're done, do this */
va_end( ap );
/* Got it? */
return 0;
}
Most of the junk that is in a format string for such functions
is parsed inside the function too, so you get the idea.

-- The Dragon


Nov 14 '05 #4


Alex wrote:
Thanks for the reply!

How do you determine when you've reached the end of the arguement list?

say you parse 5 arguements to a function, but the code in the function is
processing
each arguement till it reaches the end, how could code be written to check
if the arguement
being read, is the last one?
Please do not top-post. Write your answer below the text you are
referring to.
There are essentially 3 ways to communicate the number of arguments

- explicit: one of the mandatory arguments is the number of
(optional or total) arguments. Example:
double sumN (int N, ... /* doubles */)
{
/* N>=0, sums up the optional arguments which are all
** of type double*/
}
- semi-explicit: one of the mandatory arguments contains
information which gives you the number of optional arguments.
Example:
int printf(const char*restrict format, ...)
{
/* Note: restrict is a C99 keyword */
}
- implicit: the last argument has a certain form which tells
you that it is the last. Example:
/* UNIX system call for a program, <unistd.h> */
int execl(const char *path, const char *arg, ... /* strings */);
/* Explanation from the c.l.c FAQ 5.2:
For example, the Unix system call
execl takes a variable-length, null-pointer-terminated list of
character pointer arguments, and is correctly called like this:
execl("/bin/sh", "sh", "-c", "date", (char *)0);
That is, the last argument is a null pointer of type (char *).
*/

Note that in the first and last case, we restricted ourselves to
a certain type for the variable arguments. The format string of
printf also gives us the flexibility for arbitrary argument types.
You can extend and/or combine the three ideas, but in the end, it
comes down to those.
Cheers
Michael

"The Dragon" <no*****@nospam .com> wrote in message
news:nc******** ************@ro gers.com...
Alex wrote:
Can anyone point me to any good online references
about the va_arg() va_list() functions/macros

I googled and found a couple of things, but what I found, failed to
explain
how it really works and give a easy to follow example program

Any pointers appreciated.


#include <stdarg.h>

/* yadda yadda, main, and prototypes, and all that jazz */
int foo_varargs( char *fmt, ... ) {
va_list ap, bp;
int foo;

/* This starts the "va_*" functions up, the format
is your va_list, and the last argument defined before ... */
va_start( ap, fmt );
/* Now, you can get an arg from the list by type etc... It will
forward to the next argument, and va_arg will automagically cast
to "type", (in this case int), hence why you can pass
an integer where you should have passed a string where you
defined %s in the format string, and endup with a GPF, or
segv ;} */
foo = va_arg( &ap, int );
/* Or copy one list to another... */
va_copy( bp, ap );
/* And when you're done, do this */
va_end( ap );
/* Got it? */
return 0;
}
Most of the junk that is in a format string for such functions
is parsed inside the function too, so you get the idea.

-- The Dragon


--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #5
Alex wrote:
Can anyone point me to any good online references
about the va_arg() va_list() functions/macros

I googled and found a couple of things, but what I found, failed to explain
how it really works and give a easy to follow example program

Any pointers appreciated.


A good reference is www.delorie.com More specifically,
http://www.delorie.com/gnu/docs/glibc/libc_675.html

Regards,
Jonathan
Nov 14 '05 #6
Alex wrote:
Thanks for the reply!

How do you determine when you've reached the end of the arguement list?

say you parse 5 arguements to a function, but the code in the function is
processing
each arguement till it reaches the end, how could code be written to check
if the arguement
being read, is the last one?


Well, I dont see why you'd bother, since generally these functions have
a format that comes through with it, but if yours doesn't, then it
appears you might be up a stump.

From a UNIX manpage:

The va_arg macro expands to an expression that has the type and

value of the next argument in the call. The parameter ap is
the va_list ap initialized by va_start. Each call to va_arg
modifies ap so that the next call returns the next argument.

The parameter type is a type name specified so that the type of a
pointer to an object that has the spec-ified type can be obtained
simply by adding a * to type.

The first use of the va_arg macro after that of the va_start
macro returns the argument after last. Successive invocations
return the values of the remaining arguments.

If there is no next argument, or if type is not compatible
with the type of the actual next argument (as promoted according
to the default argument promotions), random errors will occur.

If ap is passed to a function that uses va_arg(ap,type) then the
value of ap is undefined after the return of that function.
Ew... "Random Errors"... that doesn't sound pleasant. My suggestion?
Either pass an arg count before the varargs, or pass a format string.

Then you can simply ignore extra arguments =}

- The Dragon
Nov 14 '05 #7
The Dragon <no*****@nospam .com> wrote:
: Alex wrote:
: > Can anyone point me to any good online references
: > about the va_arg() va_list() functions/macros
: >
: >
: #include <stdarg.h>

. . . . .

: int foo_varargs( char *fmt, ... ) {
: va_list ap, bp;
: int foo;

: /* This starts the "va_*" functions up, the format
: is your va_list, and the last argument defined before ... */
: va_start( ap, fmt );

Well, what this does is to set ap -> fmt. Note that the argument-list must
be non-empty (containing at least one fixed argument). Usually, va_start()
is used to set ap -> the final fixed argument.

: /* Now, you can get an arg from the list by type etc... It will
: forward to the next argument, and va_arg will automagically cast
: to "type", (in this case int), hence why you can pass
: an integer where you should have passed a string where you
: defined %s in the format string, and endup with a GPF, or
: segv ;} */
: foo = va_arg( &ap, int );
|
You don't want the & ---' (should be: foo = va_arg(ap, int);
This updates ap to point at the int-type argument (past `fmt').
: /* Or copy one list to another... */
: va_copy( bp, ap );
: /* And when you're done, do this */
: va_end( ap );

Nov 14 '05 #8

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

Similar topics

6
3697
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
3348
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
11
3077
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
2865
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
9477
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...
2
1352
by: Bill Pursell | last post by:
In libabc, I have a function which takes a NULL-terminated array of character strings. A second, closely related function take a NULL-terminated array of void *: abc_foo( char ** ); abc_vfoo( void **); I'd like to add 4 similar functions: a variadic version that takes a char * as its first argument, a variadic version that takes a void * as its first arg, and similar versions that take a va_list:
5
4670
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
4069
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
5552
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
9720
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
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10372
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
10374
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
9193
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...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.