1STDARG(3) 386BSD Programmer's Manual STDARG(3) 2 3NNAAMMEE 4 ssttddaarrgg - variable argument lists 5 6SSYYNNOOPPSSIISS 7 ##iinncclluuddee <<ssttddaarrgg..hh>> 8 9 _v_o_i_d 10 vvaa__ssttaarrtt(_v_a__l_i_s_t _a_p, _l_a_s_t) 11 12 _t_y_p_e 13 vvaa__aarrgg(_v_a__l_i_s_t _a_p, _t_y_p_e) 14 15 _v_o_i_d 16 vvaa__eenndd(_v_a__l_i_s_t _a_p) 17 18DDEESSCCRRIIPPTTIIOONN 19 A function may be called with a varying number of arguments of varying 20 types. The include file <_s_t_d_a_r_g._h> declares a type (_v_a__l_i_s_t) and defines 21 three macros for stepping through a list of arguments whose number and 22 types are not known to the called function. 23 24 The called function must declare an object of type _v_a__l_i_s_t which is used 25 by the macros vvaa__ssttaarrtt(), vvaa__aarrgg(), and vvaa__eenndd(). 26 27 The vvaa__ssttaarrtt() macro initializes _a_p for subsequent use by vvaa__aarrgg() and 28 vvaa__eenndd(), and must be called first. 29 30 The parameter _l_a_s_t is the name of the last parameter before the variable 31 argument list, i.e. the last parameter of which the calling function 32 knows the type. 33 34 Because the address of this parameter is used in the vvaa__ssttaarrtt() macro, it 35 should not be declared as a register variable, or as a function or an ar- 36 ray type. 37 38 The vvaa__ssttaarrtt() macro returns no value. 39 40 The vvaa__aarrgg() macro expands to an expression that has the type and value 41 of the next argument in the call. The parameter _a_p is the _v_a__l_i_s_t _a_p 42 initialized by vvaa__ssttaarrtt(). Each call to vvaa__aarrgg() modifies _a_p so that the 43 next call returns the next argument. The parameter _t_y_p_e is a type name 44 specified so that the type of a pointer to an object that has the speci- 45 fied type can be obtained simply by adding a * to _t_y_p_e. 46 47 If there is no next argument, or if _t_y_p_e is not compatible with the type 48 of the actual next argument (as promoted according to the default argu- 49 ment promotions), random errors will occur. 50 51 The first use of the vvaa__aarrgg() macro after that of the vvaa__ssttaarrtt() macro 52 returns the argument after _l_a_s_t. Successive invocations return the values 53 of the remaining arguments. 54 55 The vvaa__eenndd() macro handles a normal return from the function whose vari- 56 able argument list was initialized by vvaa__ssttaarrtt(). 57 58 The vvaa__eenndd() macro returns no value. 59 60EEXXAAMMPPLLEESS 61 The function _f_o_o takes a string of format characters and prints out the 62 argument associated with each format character based on the type. 63 64 void foo(char *fmt, ...) 65 { 66 va_list ap; 67 int d; 68 char c, *p, *s; 69 70 va_start(ap, fmt); 71 while (*fmt) 72 switch(*fmt++) { 73 case 's': /* string */ 74 s = va_arg(ap, char *); 75 printf("string %s\n", s); 76 break; 77 case 'd': /* int */ 78 d = va_arg(ap, int); 79 printf("int %d\n", d); 80 break; 81 case 'c': /* char */ 82 c = va_arg(ap, char); 83 printf("char %c\n", c); 84 break; 85 } 86 va_end(ap); 87 } 88 89SSTTAANNDDAARRDDSS 90 The vvaa__ssttaarrtt(), vvaa__aarrgg(), and vvaa__eenndd() macros conform to ANSI C3.159-1989 91 (``ANSI C''). 92 93CCOOMMPPAATTIIBBIILLIITTYY 94 These macros are _n_o_t compatible with the historic macros they replace. A 95 backward compatible version can be found in the include file <_v_a_r_a_r_g_s._h>. 96 97BBUUGGSS 98 Unlike the _v_a_r_a_r_g_s macros, the ssttddaarrgg macros do not permit programmers to 99 code a function with no fixed arguments. This problem generates work 100 mainly when converting _v_a_r_a_r_g_s code to ssttddaarrgg code, but it also creates 101 difficulties for variadic functions that wish to pass all of their argu- 102 ments on to a function that takes a _v_a__l_i_s_t argument, such as 103 vfprintf(3). 104 105BSD Experimental June 29, 1991 3 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133