pelliott@hounix.uucp (Paul Elliott) (02/05/91)
In ansi C, is there a way to have a function with a variable argument list and zero fixed parameters? If so what would one use for the second parameter to va_start, which is normally supposed to be the last fixed parameter?
steve@taumet.com (Stephen Clamage) (02/07/91)
pelliott@hounix.uucp (Paul Elliott) writes: |In ansi C, is there a way to have a function with a variable argument list |and zero fixed parameters? If so what would one use for the second parameter |to va_start, which is normally supposed to be the last fixed parameter? Not in ANSI C. The <stdarg.h> method requires at least one fixed argument. -- Steve Clamage, TauMetric Corp, steve@taumet.com
dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (02/07/91)
In article <588@taumet.com> steve@taumet.com (Stephen Clamage) writes: >pelliott@hounix.uucp (Paul Elliott) writes: >|In ansi C, is there a way to have a function with a variable argument list >|and zero fixed parameters? If so what would one use for the second parameter >|to va_start, which is normally supposed to be the last fixed parameter? > >Not in ANSI C. The <stdarg.h> method requires at least one fixed argument. Probably a reasonable decision. But it does make it a pain to deal with the third party software we use that is full of <varargs.h> functions that can take 0 or more arguments. -- Dave Eisen "If I wanted to be a diplomat, I would 1447 N. Shoreline Blvd. have joined the foreign service." Mountain View, CA 94043 --- my girlfriend to her boss (415) 967-5644 dkeisen@Gang-of-Four.Stanford.EDU (for now)
gwyn@smoke.brl.mil (Doug Gwyn) (02/07/91)
In article <1991Feb5.145001.26837@hounix.uucp> pelliott@hounix.uucp (Paul Elliott) writes: >In ansi C, is there a way to have a function with a variable argument list >and zero fixed parameters? No. However, you can use a dummy fixed parameter that has no use other than in va_start().
gwyn@smoke.brl.mil (Doug Gwyn) (02/07/91)
In article <1991Feb6.171144.7182@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes: >Probably a reasonable decision. But it does make it a pain to deal >with the third party software we use that is full of <varargs.h> functions >that can take 0 or more arguments. Since the linkage for <stdarg.h> is not necessarily the same as that which was used for <varargs.h>, you have to go through the code and fix it anyway. Adding a dummy argument to those functions should not be much more work than you already have to do.
scott@bbxsda.UUCP (Scott Amspoker) (02/07/91)
In article <1991Feb6.171144.7182@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes: >Probably a reasonable decision. But it does make it a pain to deal >with the third party software we use that is full of <varargs.h> functions >that can take 0 or more arguments. Just curious: How does such a function know whether it was called with 0 or 1 argument without the aid of some external flag? Sounds like a botched up interface to me. -- Scott Amspoker | Basis International, Albuquerque, NM | This space available (505) 345-5232 | unmvax.cs.unm.edu!bbx!bbxsda!scott |
dws@margay.cs.wisc.edu (DaviD W. Sanderson) (02/07/91)
In article <1991Feb6.171144.7182@Neon.Stanford.EDU> Dave Eisen writes: >In article <588@taumet.com> Stephen Clamage writes: >>Paul Elliott writes: >>|In ansi C, is there a way to have a function with a variable argument list >>|and zero fixed parameters? >>Not in ANSI C. The <stdarg.h> method requires at least one fixed argument. >...[we use] third party software [...] that is full of <varargs.h> functions >that can take 0 or more arguments. Hmm. How would a <varargs.h> function decide that it was called with no arguments? It is not always possible to determine the number of arguments by examining the stack frame. Perhaps your third-party software relies on being able to extract this information from the stack frame? That *would* be a pain! -- ___ / __\ U N S H I N E DaviD W. Sanderson | | | I N E dws@cs.wisc.edu _____| | |_____ ________ \ / \ |__/ /////__ Fusion Powered Locomotives Made to Order \____/ \__|_/ \\\\\______ (TARDIS model available at extra cost)
enag@ifi.uio.no (Erik Naggum) (02/07/91)
In article <1991Feb6.171144.7182@Neon.Stanford.EDU>, Dave Eisen writes: > In article <588@taumet.com>, Stephen Clamage writes: >> In article <id_destroyed_by_Stephen_Clamage>, Paul Elliott writes: >>> In ansi C, is there a way to have a function with a variable argument list >>> and zero fixed parameters? If so what would one use for the second parameter >>> to va_start, which is normally supposed to be the last fixed parameter? > >> Not in ANSI C. The <stdarg.h> method requires at least one fixed argument. > > Probably a reasonable decision. But it does make it a pain to deal > with the third party software we use that is full of <varargs.h> functions > that can take 0 or more arguments. How do these functions determine how many arguments to use, and what type they are? My experience (although limited with "third party software"), is that <varargs.h>-based functions only appear to have "0 or more arguments", since the first thing they do is to get the arguments that are of known type, including some "format specifier" or other "argument type and count list". Is this not so? I found ANSI X3J11's decision to make the first argument of known type very wise. In fact, the whole <stdarg.h> thing is pretty nifty. -- [Erik Naggum] <enag@ifi.uio.no> Naggum Software, Oslo, Norway <erik@naggum.uu.no>
dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (02/07/91)
In article <1704@bbxsda.UUCP> scott@bbxsda.UUCP (Scott Amspoker) writes: >In article <1991Feb6.171144.7182@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes: >>Probably a reasonable decision. But it does make it a pain to deal >>with the third party software we use that is full of <varargs.h> functions >>that can take 0 or more arguments. > >Just curious: How does such a function know whether it was called with >0 or 1 argument without the aid of some external flag? Sounds like a >botched up interface to me. It can't. But there is an external flag --- a global variable that is used to specify what calling convention will be used. It's not the world's prettiest interface, but I really don't have any problems with it. -- Dave Eisen "If I wanted to be a diplomat, I would 1447 N. Shoreline Blvd. have joined the foreign service." Mountain View, CA 94043 --- my girlfriend to her boss (415) 967-5644 dkeisen@Gang-of-Four.Stanford.EDU (for now)
barmar@think.com (Barry Margolin) (02/07/91)
In article <ENAG.91Feb6230643@holmenkollen.ifi.uio.no> enag@ifi.uio.no (Erik Naggum) writes: >In article <1991Feb6.171144.7182@Neon.Stanford.EDU>, Dave Eisen writes: >> Probably a reasonable decision. But it does make it a pain to deal >> with the third party software we use that is full of <varargs.h> functions >> that can take 0 or more arguments. >How do these functions determine how many arguments to use, and what >type they are? Well, consider a function taking a variable number of strings, followed by a null pointer. The type is known by definition, and the number of arguments can be determined by looking at them. I'm not claiming that this is a good programming style, but an awful lot of people seem to be assuming that a fixed argument is needed in order to determine how to process the remaining arguments. While this style could easily be replaced with one where there is an initial fixed argument containing the count of variable arguments, that's more prone to error: during program maintenance it is likely that an argument will be added but the argument count could easily be left unchanged. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar
fenn@wpi.WPI.EDU (Brian Fennell) (02/07/91)
The answer to the original question appears to be, "You usually don't really need to, and here's how to fake it...." (See above) Here is an example of something that is not too far fetched that really does want 0 _fixed_type_ arguments. I am programming in X windows. most of the routines I use want to refer to a specific instance of a widget pseudo-object. This results in many lines that redundantly refer to the same widget, sometimes twenty lines in a row. I decide to use a global, much like printf uses a global called stdout. I have one routine that resets the one or two necessary parameters for the current widget, these might be integers, strings, or even in an unusual case doubles, depending on the class of my global. In this example (which I am currently working with in real life) the stdarg.h standard falls short of the vararg.h standard. Truth be told, I could use all pointers, but this makes using double constants such as M_PI a bit of a pain. I could also use a macro to pass the sizeof() as first argument but macros don't take variable number of arguments. Brian Fennell == fenn@wpi.wpi.edu
sarima@tdatirv.UUCP (Stanley Friesen) (02/08/91)
In article <1991Feb7.043715.1224@Think.COM> barmar@think.com (Barry Margolin) writes: >Well, consider a function taking a variable number of strings, followed by >a null pointer. The type is known by definition, and the number of >arguments can be determined by looking at them. > > While this style could >easily be replaced with one where there is an initial fixed argument >containing the count of variable arguments, that's more prone to error: Ah, but the style you specified above *does* have one 'fixed' argument, since all valid calls must pass *at* *least* one argument, a null pointer. (That is an attempt to call it with *no* arguments is a serious error). Furthermore this single required argument is of type (char *), even if it is the null pointer. Thus the specification: void sfunc(char *, ...); is a perfectly valid way of prototyping it. Thus the style you mentioned is in fact compatible with stdarg.h, and is best treated as requiring one character pointer argument. A varargs.h approach which pretends that there are no required arguments is actually misleading. -- --------------- uunet!tdatirv!sarima (Stanley Friesen)
enag@ifi.uio.no (Erik Naggum) (02/08/91)
In article <1991Feb7.043715.1224@Think.COM>, Barry Margolin writes: > In article <ENAG.91Feb6230643@holmenkollen.ifi.uio.no>, Erik Naggum writes: > >How do these functions determine how many arguments to use, and what > >type they are? > Well, consider a function taking a variable number of strings, > followed by a null pointer. The type is known by definition, and > the number of arguments can be determined by looking at them. > I'm not claiming that this is a good programming style, but an awful > lot of people seem to be assuming that a fixed argument is needed in > order to determine how to process the remaining arguments. While > this style could easily be replaced with one where there is an > initial fixed argument containing the count of variable arguments, > that's more prone to error: during program maintenance it is likely > that an argument will be added but the argument count could easily > be left unchanged. You mean char *strmanycat (...) which takes n char * arguments, the last of which being NULL? Since the type of the argument is known, _and_ there has to be at least one argument, why not use char *strmanycat (char *, ...) which could actually use the first argument in the same way it will use the remaining arguments? (Thanks to Mike Taylor at System Simulation, UK, for the example.) I'm left to believe that there is no real use for a (...) construct. -- [Erik Naggum] <enag@ifi.uio.no> Naggum Software, Oslo, Norway <erik@naggum.uu.no>
steve@taumet.com (Stephen Clamage) (02/09/91)
enag@ifi.uio.no (Erik Naggum) writes: >I'm left to believe that there is no real use for a (...) construct. Well, if you mean that allowing a variable number of arguments to a function is a bad idea, I wouldn't disagree. But if you mean that it should be allowed without any special construct such as ..., I disagree strongly. The ellipsis notifies the compiler (and a person reading the program) that special care must be taken in interpreting function arguments. In particular, special calling sequences may be required. On modern architectures, function arguments may be passed in registers, and the registers used may depend on the argument types. When the argument number and types are not known to the called routine, the arguments must be placed in a location which can be determined by the called routine. If notification is not required when a routine takes a variable number of arguments, the inefficient calling sequence must be used for *all* routines, when usually very few are variadic. The <varargs.h> model in Standard C, including a required fixed argument, was developed to ensure that variadic functions could be implemented on a wide variety of architectures. -- Steve Clamage, TauMetric Corp, steve@taumet.com