This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: can't pass char through va_list


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.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]