Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!bellcore!faline!ulysses!allegra!alice!ark From: ark@alice.UUCP Newsgroups: comp.unix.wizards Subject: Re: (simple?) varargs question Message-ID: <7559@alice.UUCP> Date: 22 Dec 87 01:55:11 GMT References: <10937@brl-adm.ARPA> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 48 I hope the following helps. Every function that can be called with a variable number of arguments must have the following form: #include T F (va_alist) va_dcl { B } Here T represents the type of the function's return value, F represents the name of the function, and B represents the body of the function. Everything else must appear as written above, including the lack of a semicolon after `va_dcl'. Within the body of this function, you may declare one or more objects of type va_list (not va_alist). Such an object represents a "pointer" into the argument list. If v is an object of type va_list, the following operations are defined on v: va_start (v); va_end (v); va_arg (v, AT) Before v is used, va_start(v) must be executed. A function that executes va_start(v) should execute va_end(v) before returning. Both va_start and va_end must be executed in the function that has va_dcl in its header. The va_arg macro says the following: "I know that v is now pointing at an argument of type AT. Fetch that argument and return it as your value. Also increment v to point to the next argument." It is the programmer's responsibility to ensure that the next argument exists and is indeed of type AT. Since char and short arguments are automatically promoted to int, AT must not be `char' or `short'. The va_arg macro need not physically appear in the function with the variable argument list. Indeed, the first argument of va_arg need not be the name of a va_list variable at all -- it can be any expression that evaluates to a va_list lvalue. In particular, a function with a variable argument list may pass the address of an initialized va_arg object to some other function, which uses va_arg on it with or without copying it.