Re: var_args problems in C... any ideas?

Christopher Fraser (
Mon, 12 Dec 1994 23:54:58 +1100 (EST)

But I thought Paul Damien Mclachlan said:
> OK... here's a toughie...
> OK... I'm trying to write a function that is close to scanf, but
> horribly more complex once you look at it. (I think so, anyway)
> consider:
> void interface( char *typelist, char *instring, void *function )
> where typelist is a scanf type string, eg "%c%d%s"
> instring is a list to scan from, eg "a14hello there"
> and function is a function to call. In the above example it would be:
> void function( char g, int num, char *str );
> see what I mean? I need a way to generate a var_args style list,
> without actually using var_args, because they are just used for
> reading varargs, not passing them, as far as I can tell.

Having never written a var_args program in my life, I can happily
say I have no idea, although I suspect the only portable
way to build a var_args list is to get the compiler to do it
for you (not useful in this case). However, I can suggest you abandon
the var_args approch and use a switch, eg:

switch (num_args) {
case 0:
case 1:
case 2:
(*f)(arg1, arg2);

/* ... */


which will probably cause you much less grief.

> I'd preferably like a way to do it without having to consider
> each possible permutation of the typelist string, and I'd really
> like to be told it can be done in portable C... I could do it on
> a PC, with stack based passing using just a little hack - but t>
> hats not very portable - and I wouldn't have a clue how to do it
> so that it would work on ftoomsh anyway!
> btw... the actual use of this code is very good - this is a highly
> simplified example, but anyway :P
> I'd appreciate any help from all you clever ppl out there.... because
> I've spent a day on it and don't have a clue!

Give up! Don't do it! Pass arrays if you have to! Abandon all hope!

Does that help?