va_list and gtk



Following up myself:
 > One thing about gtk: the Watcom compiler (argh, maybe I should switch
 > to using only MSVC and forget Watcom) defines va_list as "typedef char
 > *va_list[1]". As you might imagine, va_list being an array type causes
 > all kinds of strange effects in gtk in the functions where va_lists
 > are passed by reference....

 > What does the C standard say, what should you be allowed to do with a
 > va_list? Are there other compilers that define va_list as an array? Is
 > the Watcom compiler non-compliant in this way?

(If you wonder what this has to do with gtk, see for instance
gtksignal.c which passes a pointer to a va_list.)

It seems that at least the PowerPC also uses an array as va_list. See
for instance http://www.linuxppc.org/~sbsams/oldlist3/0073.html, or
va-ppc.h on any gcc installation.

Apparently having va_list be an array type is quite legal, see for
instance http://www.spots.ab.ca/~i/c/stdarg.html. (This page just
happened to be returned by an AltaVista search.) This is a bit
strange as this means that the following simple program works
differently with compilers where va_list is an array.

#include <stdio.h>
#include <stdarg.h>

void
zap(va_list *ap)
{
  int k;
  k = va_arg(*ap, int);
  printf("zap: got k = %d\n", k);
}

void
bar(va_list ap)
{
  int k;
  k = va_arg(ap, int);
  printf("bar: got k = %d\n", k);
}

void
ugh(va_list ap)
{
  zap(&ap);
}

void
foo(int i, ...)
{
  va_list ap;

  va_start(ap, i);
  bar(ap);
  bar(ap);
  va_end(ap);

  va_start(ap, i);
  ugh(ap);
  va_end(ap);

  va_start(ap, i);
  zap(&ap);
  zap(&ap);
  va_end(ap);
}

main(int argc, char **argv)
{
  foo(42, 567, 789, 890);
  return 0;
}

When compiled with MSVC 5.0 (on NT) or gcc (on HP-UX or NT), this
program prints:

bar: got k = 567
bar: got k = 567
zap: got k = 567
zap: got k = 567
zap: got k = 789

When compiled with Watcom 10.6, the compiler warns about line (23):
"Warning! W100: Parameter 1 contains inconsistent levels of
indirection", but still compiles it, and the program prints:

bar: got k = 567
bar: got k = 789
zap: got k = 1244844
zap: got k = 567
zap: got k = 789

--tml



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]