473,473 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Create 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 2285
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********************@rogers.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.a u...
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********************@rogers.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********************@rogers.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
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...
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...
2
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(...
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: 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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.