passing variable numbers of arguments
David MacKenzie
mackenzi at agnes.uucp
Sat Jan 7 15:20:11 AEST 1989
The following program produces unexpected results:
#include <varargs.h>
main ()
{
printf ("foo:\n");
foo (1, 2, 3, 0);
printf ("bar:\n");
bar (4, 5, 6, 0);
}
foo (va_alist)
va_dcl
{
bar (va_alist);
}
bar (va_alist)
va_dcl
{
va_list list;
int i;
va_start (list);
while (i = va_arg (list, int))
printf ("%d\n", i);
va_end (list);
}
On a 68000 machine, it produces:
foo:
1
14679744
160
1
2
3
bar:
4
5
6
and on a VAX it produces:
foo:
1
bar:
4
5
6
whereas the output I wanted is:
foo:
1
2
3
bar:
4
5
6
In other words, if I call bar () directly, it works, but if I call it via
foo (), it breaks, in an implementation-dependant way, no less. Why?
Is there anyway to get this to work? The problem has arisen as I
am trying to port Allan Holub's (DDJ, April 1988) integer-only
printf from <stdarg.h> to <varargs.h>. printf, sprintf, and fprintf
are small little functions that call a function called idoprnt and both the
small front-end functions and idoprnt seem to need to accept a variable
number of arguments.
David MacKenzie
edf at rocky2.rockefeller.edu
---
p.s. I post from this account because inews doesn't seem to be able to post
new articles on rocky2 -- very strange, since it can post followups just
fine.
More information about the Comp.lang.c
mailing list