help with va_list etc

Discussion in 'C Programming' started by Alex, Dec 30, 2004.

  1. Alex

    Alex Guest

    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.
    Alex, Dec 30, 2004
    #1
    1. Advertising

  2. Alex

    The Dragon Guest

    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
    The Dragon, Dec 30, 2004
    #2
    1. Advertising

  3. Alex

    Alex Guest

    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" <> wrote in message
    news:...
    > 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
    Alex, Dec 30, 2004
    #3
  4. Alex

    Mike H Guest

    "Alex" <> wrote in message
    news:41d3b1d3$0$11881$...
    > 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" <> wrote in message
    > news:...
    >> 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

    >
    >
    Mike H, Dec 30, 2004
    #4
  5. Alex

    Michael Mair Guest

    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" <> wrote in message
    > news:...
    >
    >>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.
    Michael Mair, Dec 30, 2004
    #5
  6. 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
    Jonathan Burd, Dec 31, 2004
    #6
  7. Alex

    The Dragon Guest

    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
    The Dragon, Jan 1, 2005
    #7
  8. The Dragon <> 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 );
    Scott J. McCaughrin, Jan 2, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter

    va_list help, please ...

    Peter, Feb 15, 2005, in forum: C++
    Replies:
    6
    Views:
    3,536
    Pete Becker
    Feb 15, 2005
  2. John Guo

    va_list help

    John Guo, Mar 31, 2005, in forum: C++
    Replies:
    5
    Views:
    3,308
    Pete Becker
    Mar 31, 2005
  3. Robin Cull
    Replies:
    5
    Views:
    391
    Andrew Dalke
    Jul 31, 2003
  4. Daniel Joyce

    Python Audio (Alpy, Fastaudio, Etc Etc)

    Daniel Joyce, Sep 16, 2003, in forum: Python
    Replies:
    1
    Views:
    904
    Markus Wankus
    Sep 16, 2003
  5. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    368
    Fredrik Lundh
    Aug 13, 2008
Loading...

Share This Page