Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>va_arg() access variable number of arguments, ansi c style</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 va_arg()                Access Variable Number of Arguments, ANSI C Style

 #include   <stdarg.h>;                  Required for compatibility with ANSI
                                         C standard

 void       va_start(arg_ptr,prev_param); Set 'arg_ptr' to beginning of list
 va_list    arg_ptr;                     Pointer to list of arguments
 type       prev_param;                  Parameter preceding first optional
                                          argument

 type       va_arg(arg_ptr,type);        Retrieve current argument from list
 va_list    arg_ptr;                     Pointer to list of arguments
 type       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.

     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'.)
                    'prev_param' is the name of the required parameter
                    immediately preceding the first optional argument.
                    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.

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

    The following statements add variable lists of integers.

           #include <stdarg.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(first)
           int first;
           {
               va_list arg_ptr;
               int x, subt;

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


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

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