Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!dayton!viper!john From: john@viper.UUCP Newsgroups: comp.sys.atari.st Subject: Re: "file selector" blues, Megamax bug Message-ID: <826@viper.UUCP> Date: Mon, 13-Apr-87 01:20:47 EST Article-I.D.: viper.826 Posted: Mon Apr 13 01:20:47 1987 Date-Received: Tue, 14-Apr-87 00:46:26 EST References: <639@batcomputer.tn.cornell.edu> <821@viper.UUCP> Reply-To: john@viper.UUCP (John Stanley) Organization: DynaSoft Systems Lines: 57 Keywords: user-friendly C sizeof In article <821@viper.UUCP> I wrote >In article <639@batcomputer.tn.cornell.edu> > braner@tcgould.tn.cornell.edu.UUCP (braner) writes: > > > >Yet another bug in Megamax C: > > > > sizeof("a string constant") always yields 4. > > > > (sizeof(s) where s is of type array-of-char works fine.) > > > >- Moshe Braner > > When you ask for sizeof(something) you're always asking how large >that element is to the compiler. The type reference is a reference >to a type with a specific size... The reference to the string is >translated into a pointer before sizeof gets around to doing it's job. >I agree that the latter is confusing, but it conforms to K&R page 94 >"... a reference to an array is converted by the compiler to a pointer >to the beginning of the array." And to page 188 where the effects of >using sizeof on a type identifier is described. If you wanted to know >how long the string is, you might want to use strlen instead... > Well... I've looked in 5 different references and found 2 different ways to interpret how sizeof("string") should end up being evaluated. The "Bible" (K&R) doesn't give any examples that directly apply (although there are a few examples in the other books I have that -imply- that it should result in the length of the string plus one (for the trailing null)). I'll go so far here as to say I was probably wrong. It looks like someone at Megamax took a look at K&R and made the same easy-to-make mistake that I did. The quote that I refered to from page 94 implys that the conversion is done before evaluation of an expression is started. Unfortunately, the documentation covering sizeof also implys that the sizeof operation must be done during the evaluation phase which is -after- the reference to the array (any array) would be converted to a pointer... This implys that the the value returned from sizeof("string") would be the size of the pointer, not the size of the string. The second case which involves an array directly implys that whoever implemented the sizeof got the code right and the person who wrote the code to process a string overlooked the need to maintain the array-size information for the single case where it would matter... This is another example of why an ANSI standard for C is necessary. I sincerly hope this is explicitly detailed so we won't run into this problem in ANSI-conforming compilers.... Oh well... Live and learn... Why are you using an -odd- construct like that anyway? --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john