help with va_list etc

A

Alex

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.
 
T

The Dragon

Alex said:
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
 
A

Alex

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?
 
M

Mike H

Alex said:
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.
 
M

Michael Mair

Alex said:
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
 
T

The Dragon

Alex said:
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
 
S

Scott J. McCaughrin

: 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 );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top