Path: utzoo!attcan!uunet!husc6!uwvax!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: passing variable numbers of arguments Message-ID: <15341@mimsy.UUCP> Date: 7 Jan 89 21:16:53 GMT References: <899@thor.stolaf.edu> Distribution: na Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 53 In article <899@thor.stolaf.edu> mackenzi@agnes.uucp (David MacKenzie) writes: >The following program produces unexpected results: >foo (va_alist) > va_dcl >{ > bar (va_alist); >} This program is quite thoroughly illegal. `va_alist' is allowed to be defined as a `magic token' that tells the compiler `I accept variable argument lists'; this magic token may well not be a valid argument. If you get lucky, the compiler will object to the call to bar(). >In other words, if I call bar () directly, it works, but if I call it via >foo (), it breaks, in an implementation-dependant way, no less. Why? Because indirect calls to bar() can never pass a variable argument list. >Is there anyway to get this to work? You cannot call bar() from foo() (except with a fixed argument list). What you CAN do is call a common routine from both foo() and bar(): foo(va_alist) va_dcl { va_list ap; va_start(ap); vbar(ap); va_end(ap); } bar(va_alist) va_dcl { va_list ap; va_start(ap); vbar(ap); va_end(ap); } vbar(ap) va_list ap; { ... code to deal with variable arguments ... } This is why vprintf, vfprintf, and vsprintf exist at all. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris