Path: utzoo!attcan!uunet!husc6!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: Passing variable #of args Message-ID: <9466@smoke.BRL.MIL> Date: 21 Jan 89 14:16:23 GMT References: <470@marob.MASA.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 31 In article <470@marob.MASA.COM> daveh@marob.masa.com (Dave Hammond) writes: >What potential problems exist if A() passes *more* than 3 args to B()? >It seems to me that this would harm nothing, since the extra stuff >ends up beyond the current stack position. Stack? What stack? The C language doesn't say anything about a stack. In fact there are reasonable implementations that (a) pass arguments differently for different parameter types (b) use different mechanisms for passing different numbers of arguments. >I was surprised to see the following in a "GNU-something" source file: >lprintf(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,10) >char *fmt; >{ > char buf[BUFSIZ]; > sprintf(buf,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,10) >} >Given the availability of varargs, is this style still acceptable and, >more importantly, is it portable? That style never WAS acceptable; it relies on implementation-dependent kludgery and will not work on some implementations. Before there was , this technique was resorted to fairly heavily because it happened to work (most of the time) on the local implementation and there was no better recourse. The proper way to do this is with or macros, passing the va_list to vsprintf(). Of course, some systems don't have vsprintf(), but the solution there is to implement vsprintf() (it's just a few lines of code) and add it to the local set of C library extensions.