Path: utzoo!mnetor!uunet!iscuva!jimc From: jimc@iscuva.ISCS.COM (Jim Cathey) Newsgroups: comp.sys.mac Subject: Re: sprintf() bug? Message-ID: <1131@iscuva.ISCS.COM> Date: 2 Feb 88 17:26:09 GMT References: <4VzXREy00WABE7k0FU@andrew.cmu.edu> <5349@cit-vax.Caltech.Edu> <943@cadre.dsl.PITTSBURGH.EDU> Organization: ISC Systems Corporation, Spokane, WA Lines: 51 Keywords: C,sprintf,strings In article <943@cadre.dsl.PITTSBURGH.EDU> cgw@cadre.dsl.pittsburgh.edu.UUCP (Gray Watson) writes: >> sprintf(tempstring, "%s %f %s %f", "\pthe square of ", x, "is", (x *x)); >> DrawString(tempstring); > >Article <5349@cit-vax.Caltech.Edu> stated that the lines above "will not work >in any C which [he] understand[s]". Tempstring should, according the article >be equal to "\016the square of" followed by random garbage. > The \p (which for non-Mac routines does insert a \016) is needed for >the Mac interface. The first character in the string you send to DrawString >should be the value of the length of the string or should be \p or \016: > >If you are setting up a window title to be "Hello Dolly" the string you > should give the OpenWindow call (or whatever it is) should be > "\011Hello Dolly" with \011 being the length of "Hello Dolly", > but "\pHello Dolly" saves you from recounting the length of the string > if you change it a lot. To add to the confusion... It is my understanding that in Aztec C (the first place I saw the \p notation) something like "\pfoobar" told the C compiler to count the length of the quoted string and place it in an extra byte at the front of the string. The string was still null terminated as well. Thus "\pthe square of " yielded a hidden 16-byte storage area initialized with "\016the square of \0" (don't forget that \0xx is OCTAL). So, the sprintf example above should cough up the storage area tempstring filled with "\016the square of xxxxxx is yyyyyy\0" which would draw on the screen "the square of " since the \016 would tell DrawString to use only 14 characters out of tempstring. The correct way would be to use the CtoPStr function against tempstring, forgetting the \p stuff completely (since this scenario is exactly what this function is for). The \p notation is for string literals that aren't to be massaged later by C string functions before being sent to the Toolbox. Of course, LSC might do something differently, like it could treat \p as a literal constant character that glue versions of DrawString (et. al.) could notice and then do internal CtoPStr operations (gackkk!). This is somewhat in accord with how I read the second poster's information. I don't think LSC does this though, though I don't have it available to check. I thought the manual for Aztec C was reasonably clear on the whole matter, although I don't remember it it used examples. +----------------+ ! II CCCCCC ! Jim Cathey ! II SSSSCC ! ISC Systems Corp. ! II CC ! TAF-C8; Spokane, WA 99220 ! IISSSS CC ! UUCP: uunet!iscuva!jimc ! II CCCCCC ! (509) 927-5757 +----------------+ "With excitement like this, who is needing enemas?"