[This message was originally sent on May 27, 2001, to someone who was asking about constructing variable-length argument lists. I have edited the text slightly for this web page.]

From: scs@eskimo.com (Steve Summit)
Date: Sun, 27 May 2001 10:49:32 -0500
Message-Id: <2001May27.1049.scs.009@aeroroot.scs.ndip.eskimo.net>
Subject: Re: Variable Argument lists in C

You wrote:
> Hi, I was searching on Google for a way to build a variable of type
> va_list. My task is to accept a variable number of parameters with
> one function, remove a few arguments from that list, and then pass the
> va_list to another function, probably vprintf or vsprintf...
> Do you know how this could be accomplished? I either need
> to simply remove the args from the va_list that I obtain from the
> first function call, or build a new va_list and omit the unwanted args.

Well, there are two very different answers to this question. The first is that there is no portable way of doing this sort of thing at all; the "solutions" all involve either assembly language or ghastly kludges, or both. If at all possible, you should find some other way of accomplishing your higher task that doesn't involve trying to build or manipulate argument lists on the fly. Getting involved with dynamic argument lists is a lot like getting involved with absinthe: darkly exotic and stimulating at first, but destructive and mind-rotting in the end.

That's the sober, responsible answer. The second answer is that dynamic function calls are exotic and stimulating and tons of fun. But they're not easy; they do require either assembly language or ghastly kludges (or both). This is what question 15.13 in the comp.lang.c FAQ list is about; I'm appending my collection of "wacky ideas" at the end of this message. Besides the ideas there, lately I've been having mostly good luck with a set of gcc extensions: __builtin_apply_args, __builtin_apply, and __builtin_return. (I can't tell you how to use these, because I barely understand them myself, but if you're using gcc, they might be an option for you. The only documentation I know of for them is the gcc Info node "Constructing Calls".)

Steve Summit
scs@eskimo.com