Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>va_arg() access variable number of arguments, unix v style</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
va_arg()                 Access Variable Number of Arguments, UNIX V Style

 #include   <varargs.h>         Required for compatibility with UNIX V

 void       va_start(arg_ptr);       Set 'arg_ptr' to beginning of list
 va_list    arg_ptr;                     Pointer to list of arguments

 type       va_arg(arg_ptr,type);    Retrieve current argument from list
 va_list    arg_ptr;                     Pointer to list of arguments
 type;                                   Type of argument to be retrieved

 void       va_end(arg_ptr);         Reset 'arg_ptr'
 va_list    arg_ptr;                     Pointer to list of arguments

    va_start(), va_arg(), and va_end() are macros that, when used
    together, provide a way to access the arguments to a function when
    the function takes a variable number of arguments.  These macros set
    a pointer to the beginning of the optional argument list, retrieve
    arguments from the list, and reset the pointer when argument
    processing is finished.

    If the function takes a fixed number of arguments, those are declared
    as parameters and accessed through the parameter names. The last, or
    only, parameter to the function represents the list of optional
    arguments. This parameter must be named 'va_alist'. 'va_dcl' is a
    macro that is the complete declaration of the 'va_alist' parameter
    (including semicolon).  It appears where the declaration should,
    after the function definition and before the opening left brace of
    the function. (See the example below.)

  va_start()    Within the function, va_start() sets the pointer
                'arg_ptr' to the beginning of the variable argument list.
                ('arg_ptr' must be of type 'va_list'.)  va_start() must
                be used before va_arg() is used for the first time.

    va_arg()    retrieves a value of given 'type' from the location given
                by 'arg_ptr'.  'arg_ptr' is incremented to the next
                argument in the list using the size of 'type' to
                determine where the next argument begins.  va_arg() can
                be called as many times as necessary to retrieve all the
                values in the list.

    va_end()    resets the pointer to the list of arguments to NULL. It
                is called after va_arg() has retrieved all the values.
                va_end() is used to provide a normal return from the
                function and avoid possible undefined behavior.

    Returns:    va_arg() returns the current argument.  va_start() and
                va_end() do not return values.

      Notes:    There is another version of the va_...() macros.  These
                macros are defined in <stdarg.h> and conform to the
                proposed ANSI C standard.

  -------------------------------- Example ---------------------------------

    The following statements add variable lists of integers.

       #include <varargs.h>
       #include <stdio.h>

       int totl, totltoo;

       main()
       {
           totl = sum(50,100,25,200,1000,0);
           printf("subtotal: %d\n",totl);
           totltoo = sum(totl,75,15,0);
           printf("total: %d\n",totltoo);

       }

       sum(va_alist)
       va_dcl
       {
           va_list arg_ptr;
           int x, subt = 0;

           va_start(arg_ptr);
           while ((x = va_arg(arg_ptr,int)) > 0)
                 subt += x;
           va_end(arg_ptr);
           return(subt);
       }

See Also: vfprintf() vsprintf() vprintf()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson