STDARG(3) BSD Programmer's Manual STDARG(3) NNAAMMEE ssttddaarrgg - variable argument lists SSYYNNOOPPSSIISS ##iinncclluuddee <<ssttddaarrgg..hh>> _v_o_i_d vvaa__ssttaarrtt(_v_a___l_i_s_t _a_p, _l_a_s_t); _t_y_p_e vvaa__aarrgg(_v_a___l_i_s_t _a_p, _t_y_p_e); _v_o_i_d vvaa__eenndd(_v_a___l_i_s_t _a_p); DDEESSCCRRIIPPTTIIOONN A function may be called with a varying number of arguments of varying types. The include file <_s_t_d_a_r_g_._h> declares a type (_v_a___l_i_s_t) and defines three macros for stepping through a list of arguments whose number and types are not known to the called function. The called function must declare an object of type _v_a___l_i_s_t which is used by the macros vvaa__ssttaarrtt(), vvaa__aarrgg(), and vvaa__eenndd(). The vvaa__ssttaarrtt() macro initializes _a_p for subsequent use by vvaa__aarrgg() and vvaa__eenndd(), and must be called first. The parameter _l_a_s_t is the name of the last parameter before the variable argument list, i.e. the last parameter of which the calling function knows the type. Because the address of this parameter is used in the vvaa__ssttaarrtt() macro, it should not be declared as a register variable, or as a function or an ar- ray type. The vvaa__ssttaarrtt() macro returns no value. The vvaa__aarrgg() macro expands to an expression that has the type and value of the next argument in the call. The parameter _a_p is the _v_a___l_i_s_t _a_p initialized by vvaa__ssttaarrtt(). Each call to vvaa__aarrgg() modifies _a_p so that the next call returns the next argument. The parameter _t_y_p_e is a type name specified so that the type of a pointer to an object that has the speci- fied type can be obtained simply by adding a * to _t_y_p_e. If there is no next argument, or if _t_y_p_e is not compatible with the type of the actual next argument (as promoted according to the default argu- ment promotions), random errors will occur. The first use of the vvaa__aarrgg() macro after that of the vvaa__ssttaarrtt() macro returns the argument after _l_a_s_t. Successive invocations return the values of the remaining arguments. The vvaa__eenndd() macro handles a normal return from the function whose vari- able argument list was initialized by vvaa__ssttaarrtt(). The vvaa__eenndd() macro returns no value. EEXXAAMMPPLLEESS The function _f_o_o takes a string of format characters and prints out the argument associated with each format character based on the type. void foo(char *fmt, ...) { va_list ap; int d; char c, *p, *s; va_start(ap, fmt); while (*fmt) switch(*fmt++) { case 's': /* string */ s = va_arg(ap, char *); printf("string %s\n", s); break; case 'd': /* int */ d = va_arg(ap, int); printf("int %d\n", d); break; case 'c': /* char */ c = va_arg(ap, char); printf("char %c\n", c); break; } va_end(ap); } SSTTAANNDDAARRDDSS The vvaa__ssttaarrtt(), vvaa__aarrgg(), and vvaa__eenndd() macros conform to ANSI C X3.159-1989 (``ANSI C ''). CCOOMMPPAATTIIBBIILLIITTYY These macros are _n_o_t compatible with the historic macros they replace. A backward compatible version can be found in the include file <_v_a_r_a_r_g_s_._h>. BBUUGGSS Unlike the _v_a_r_a_r_g_s macros, the ssttddaarrgg macros do not permit programmers to code a function with no fixed arguments. This problem generates work mainly when converting _v_a_r_a_r_g_s code to ssttddaarrgg code, but it also creates difficulties for variadic functions that wish to pass all of their argu- ments on to a function that takes a _v_a___l_i_s_t argument, such as vfprintf(3). 4.4BSD June 5, 1993 2