问题
I have to change this code fragment from varargs.h to stdarg.h, but I do not know exactly how to:
#ifndef lint
int ll_log (va_alist)
va_dcl
{
int event, result;
LLog *lp;
va_list ap;
va_start (ap);
lp = va_arg (ap, LLog *);
event = va_arg (ap, int);
result = _ll_log (lp, event, ap);
va_end (ap);
return result;
}
When I try build this, compiler says:
error "GCC no longer implements <varargs.h>."
error "Revise your code to use <stdarg.h>."
The program, which I need to compile and run, has a few similar fragments and I need to know how to change them. If you can write some example, I'll be content.
回答1:
<varargs.h>
is a pre-standard C header; use <stdarg.h>
instead. The differences:
- The function must take at least one named argument.
- The function must be prototyped (using the ellipsis terminator).
- The
va_start
macro works differently: it takes two arguments, the first being theva_list
to be initialized and the second the name of the last named argument.
Example:
int ll_log (LLog *llog, ...) {
int event, result;
LLog *lp;
va_list ap;
va_start (ap, llog);
lp = llog;
event = va_arg (ap, int);
result = _ll_log (lp, event, ap);
va_end (ap);
return result;
}
Regarding va_start
: gcc ignores the second argument, but not giving the correct one is not portable.
回答2:
You have to include
#include <stdarg.h>
va_ Macro syntax stays the same.
来源:https://stackoverflow.com/questions/24950362/gcc-no-longer-implements-varargs-h