9

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.

0
11

<varargs.h> is a pre-standard C header; use <stdarg.h> instead. The differences:

  1. The function must take at least one named argument.
  2. The function must be prototyped (using the ellipsis terminator).
  3. The va_start macro works differently: it takes two arguments, the first being the va_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.

4

You have to include

#include <stdarg.h>

va_ Macro syntax stays the same.

3
  • This is correct. I'd also add that you can now use the int ll_log (...) { syntax, which wasn't available with <varargs.h> – Timothy Jones Jul 25 '14 at 7:39
  • 4
    Wikipedia actually has a nice description of the change – Timothy Jones Jul 25 '14 at 7:40
  • 1
    The macro syntax is not the same. Even ignoring the non-appearance of va_dcl in the <stdarg.h> system, the va_start() macro is different, taking one argument in the old <varargs.h> system and two in the new(er) <stdarg.h> system. – Jonathan Leffler May 23 '17 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.