Variadic argument functions (Was Re: cdecl keyword)

Andrew Scott andrew at teletron.UUCP
Thu Apr 14 03:45:59 AEST 1988


In article <7667 at brl-smoke.ARPA>, gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
> In article <5980 at utcsri.UUCP> flaps at utcsri.UUCP (Alan J Rosenthal) writes:
> >In C it is permissible to call any function (including
> >user-defined ones) with the wrong number of arguments, so long as any
> >arguments not actually passed are not accessed by the function being
> >called.
> 
> Not true in general.  If you read Dennis's anti-noalias diatribe,
> near the end you may recall that he identified two botches in original
> C, one of which was that printf()-like functions existed without
> adequate language support.  The reason for the ,... approach of ANSI
> C is to provide adequate language support for variadic arguments.

I am working on a bit of code in which I call a vectored function.  The code
to invoke the vector goes something like this:

	void (*vector)();
	int arg;

	(*vector)(arg);

The integer argument is available to the function if necessary; sometimes the
vectored function doesn't need the argument.

Now, a question related to the above comments.  Is the following code "bad"?
That is, should I re-work it so that all possible functions assigned to the
vector have exactly one integer argument, as to be consistent with the vector
call?

	void funcA(arg)
	int arg;
	{
		/* do something */
	}

	void funcB()
	{
		/* do something else */
	}

	...

	vector = funcA;		/* this is fine */
	vector = funcB;		/* is this? */

I'm hoping that I can leave it as it is, because funcA() and funcB() are library
functions, and I wouldn't want to have kludgey function argument interfaces with
unused arguments, etc.

Also, I have a totally unrelated question.  In making a library of functions, I
have run into a couple of problems with the argument interface to some func-
tions.  Specifically, suppose one (integer) argument is range limited to values
in the range 0..1000.  Should the argument be of type short, to emphasize the
range of the argument?  Or, should I use ints everywhere and simply document
the range of acceptable argument values.  How about function return values?
If the value is also range limited, should I use a smaller type or just docu-
ment the range of return values so that a user of the function can cast it into
a smaller type for storage purposes if desired.  I was looking to UNIX libraries
for inspiration; the aproach there seems to be "use int for everything except
use long for large values".

Thanks.
-- 
Andrew Scott
andrew at teletron.uucp	or	..alberta!teletron!andrew



More information about the Comp.lang.c mailing list