Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!uunet!philmtl!ray From: ray@philmtl.philips.ca (Ray Dunn) Newsgroups: comp.lang.c Subject: Re: varargs -> varargs mystery Message-ID: <1991May16.173145.2440@philmtl.philips.ca> Date: 16 May 91 17:31:45 GMT References: <4606@skye.ed.ac.uk> <3110@cirrusl.UUCP> <16068@smoke.brl.mil> Organization: Philips Electronics Ltd. Product Group PC. Montreal. Lines: 41 In referenced article, gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <3110@cirrusl.UUCP> Rahul Dhesi writes: >>Briefly, the question was: if A and B are varags functions, how can >>one portably write them so that A uses one argument and passes the rest >>to B? The short answer is: you can't. > >Sure you can, but you have to pass the remaining arguments via a va_list >parameter, not scattered about. I do this all the time. What I've found this means in practice is that when you write a varargs function it's usually best to write it as *two* functions, fn and vfn, where fn creates the va_list and passes it vfn where the work is actually done. Both fn and vfn can then be called as required. void fn(int arg, ...) { va_list argsp; va_start(argsp, arg); vfn(arg, argsp); va_end(argsp); } void vfn(int arg, va_list argsp) { /* body of code */ } Note that the example in the FAQ list defines a 'v' named function, vstrcat, as taking a variable number of arguments not a va_list. It's better style to keep to the implied convention that 'v' named functions take a va_list. -- Ray Dunn. | UUCP: ray@philmtl.philips.ca Philips Electronics Ltd. | ..!{uunet|philapd|philabs}!philmtl!ray 600 Dr Frederik Philips Blvd | TEL : (514) 744-8987 (Phonemail) St Laurent. Quebec. H4M 2S9 | FAX : (514) 744-9550 TLX: 05-824090