Path: utzoo!attcan!uunet!husc6!uwvax!rutgers!columbia!garfield!aronoff From: aronoff@garfield (Avram Aronoff) Newsgroups: comp.sys.ibm.pc Subject: Re: MS-C 5.0 var_args bug? Message-ID: <5696@columbia.edu> Date: 13 Jun 88 07:51:26 GMT References: <2664@bigtex.uucp> Sender: nobody@columbia.edu Reply-To: aronoff@garfield.UUCP (Avram Aronoff) Organization: Columbia University CS Department Lines: 25 In article <2664@bigtex.uucp> james@bigtex.uucp (James Van Artsdalen) writes: >I am attempting to use the ANSI var_args support in MS-C 5.0... > >The problem is that Microsoft used sizeof()... >...In particular, sizeof(char) is 1 >...but... >it occupies two bytes on the stack. Hence, if the fixed parameter in your >function is a char, or any but the last variable argument is char, the >Microsoft macros fail. > >/* ... */ char c; /* ... */ c = va_arg(ap, char); /* ... */ The problem lies not in the Microsoft macros, but in your code (WS :-) Since the char parameter is part of a va_list, there is no prototype that says it's being passed as a char. Therefore, it is widened to int by the standard conversions before it is passed as a parameter. In general, chars, shorts, and floats can not be used in va_arg. Instead, use int or double, and if necessary, truncate by cast or assignment. As far as structures, this would be a problem only if sizeof(struct a) was different from its size on the stack. Hmmm... (Mad dash to try struct { char a; } and struct { char a[3]; } :-) Hymie