can't pass char through va_list

Franz Sirl Franz.Sirl-kernel@lauterbach.com
Thu May 4 12:30:00 GMT 2000


Am Thu, 04 May 2000 schrieb Jon Wilkening:
>Hello,
>  The following code doesn't work right with gcc-2.95.2
>running on a sun ultra-1 under solaris 2.7:
>
>#include <stdio.h>
>#include <stdarg.h>
>
>void test(int d, ...) {
>  va_list ap;
>  char buf[4]={'a','b','c','\0'};
>    
>  va_start(ap, d);
>  buf[2] = va_arg(ap, char);
>  printf("%s\n", buf);
>  va_end(ap);
>}
>
>main() {
>  test(1, '7');
>}
>
>
>  The result should be
>
>ab7
>
>  but instead I get
>
>ab
>
>  It works correctly on an intel pentium III running red hat linux 6.0,
>or if you change char to int in va_arg(ap,char).  (I didn't compile
>with any flags.  just gcc va_arg.c)

You have the correct solution right before your eyes, use int not char. It's
just plain illegal C to use a not fully promoted type in va_arg(). Current
mainline gcc errors out on such constructs and so will gcc-3.0. So use

  buf[2] = (char) va_arg(ap, int);

to fix your code.

Franz.


More information about the Gcc-bugs mailing list