mm@blake.u.washington.edu (Eric Gorr) (12/10/90)
Now, my question: In BASIC, you can write the simple command: TEST = "Hello" What I want to know is how to do this in C after the code where I declared the varables. Thanx for your help..... ------------------------------------------------------------------------------ Mystery_Man ! All warfare is based on deception - Sun Tzu ! Diplomacy is the art of letting someone else have mm@ ! your war - Danielle Vare' blake.u.washington.edu! He who knows when he can fight and when he cannot will ! be victorious - Sun Tzu IBM - I Bought ! Alway mystify, mislead, and surprise the enemy Macintosh ! - Stonewall Jackson ------------------------------------------------------------------------------
Chris.Gehlker@p12.f56.n114.z1.fidonet.org (Chris Gehlker) (12/12/90)
EG> In BASIC, you can write the simple command: EG> TEST = "Hello" EG> What I want to know is how to do this in C after the code where EG> I declared the varables. strcpy(test,"Hello"); -- Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!56.12!Chris.Gehlker Internet: Chris.Gehlker@p12.f56.n114.z1.fidonet.org
CXT105@psuvm.psu.edu (Christopher Tate) (12/13/90)
Careful here: remember, the Mac expects to see Pascal-style strings most of the time (for example, when using DrawString() to write them into a window) instead of C-style strings. So, you might have to do something like strcpy(test, "\pHello"); /* \p indicates a Pascal string */ instead of strcpy(test, "Hello"); /* vanilla C string */ This is what THINK C uses to allow Pascal-string literals in the source code; I don't know if MPW uses the same convention. (BTW, if you don't know, C strings have a (char) 0 immediately after the last char in the string, as a termination mark; Pascal strings use the first byte as a length indicator -- thus, they can't be more than 255 characters of actual text.) ------- Christopher Tate | etaT rehpotsirhC Bitnet: cxt105@psuvm | mvusp@501txc :tentiB Uucp: ...!psuvax1!psuvm.bitnet!cxt105 | 501txc!tentib.mvusp!1xavusp!... :pcuU Internet: cxt105@psuvm.psu.edu | ude.usp.mvusp@501txc :tenretnI
russotto@eng.umd.edu (Matthew T. Russotto) (12/14/90)
In article <90347.101213CXT105@psuvm.psu.edu> CXT105@psuvm.psu.edu (Christopher Tate) writes: >Careful here: remember, the Mac expects to see Pascal-style strings most of >the time (for example, when using DrawString() to write them into a window) >instead of C-style strings. So, you might have to do something like > > strcpy(test, "\pHello"); /* \p indicates a Pascal string */ NONONONONONONONONO! "A string beginning with "\p" or "\P" is a Pascal string. It is not terminated with a null byte". That strcpy can copy tons of characters and write all over memory. Try strncpy(test, "\pHello", 6); -- Matthew T. Russotto russotto@eng.umd.edu russotto@wam.umd.edu .sig under construction, like the rest of this campus.
amanda@visix.com (Amanda Walker) (12/14/90)
In article <1990Dec13.183037.655@eng.umd.edu> russotto@eng.umd.edu (Matthew T. Russotto) writes: >"A string beginning with "\p" or "\P" is a Pascal string. It is not terminated >with a null byte". That strcpy can copy tons of characters and write all over >memory. Well, actually, several C compilers (including, I believe, MPW C) *do* terminate Pascal-style string literals with a null byte. However, it is probably safest not to assume that this will work. -- Amanda Walker amanda@visix.com Visix Software Inc. ...!uunet!visix!amanda -- "I was born in Iowa--I just *work* in outer space" --Star Trek IV
russotto@eng.umd.edu (Matthew T. Russotto) (12/14/90)
In article <twp4obtaia@visix.com> amanda@visix.com (Amanda Walker) writes: >In article <1990Dec13.183037.655@eng.umd.edu> russotto@eng.umd.edu >(Matthew T. Russotto) writes: > >>"A string beginning with "\p" or "\P" is a Pascal string. It is not terminated >>with a null byte". That strcpy can copy tons of characters and write all over >>memory. > >Well, actually, several C compilers (including, I believe, MPW C) *do* >terminate Pascal-style string literals with a null byte. However, it >is probably safest not to assume that this will work. The statement I put in quotes is from the THINK C manual.... -- Matthew T. Russotto russotto@eng.umd.edu russotto@wam.umd.edu .sig under construction, like the rest of this campus.
mdtaylor@Apple.COM (Mark David Taylor) (12/15/90)
In article <1990Dec13.183037.655@eng.umd.edu> russotto@eng.umd.edu (Matthew T. Russotto) writes: >> >> strcpy(test, "\pHello"); /* \p indicates a Pascal string */ > >NONONONONONONONONO! > >"A string beginning with "\p" or "\P" is a Pascal string. It is not terminated >with a null byte". That strcpy can copy tons of characters and write all over >memory. Try > >strncpy(test, "\pHello", 6); Or perhaps: strcpy(test, "Hello"); c2pstr(test); /* or CtoPstr(test), in THINK C */ Yes, it's less optimal, but also less obfuscated. Also, for the benefit of the original poster, if you just need the string to be initialized to "Hello" at the beginning, you can avoid using strcpy() by defining your string this way: char *test = "Hello"; Also note that strcpy() is not built into C; you have to get it from some library like the ANSI library, or write it yourself. If you're really desperate, you can allocate some memory for test and then just say: test[0] = 'H'; test[1] = 'e'; etc. (ugh!) Fortunately, both MPW C and THINK C come with ANSI libraries. Or, since this is comp.sys.MAC.programmer, you can use Mac toolbox calls if you don't mind using Str255's and StringHandles. Check out NewString and SetString. (Munger, too. And BlockMove.) Of course, we all know that we should limit the use of string constants in our code, right? Ideally, one should create a 'STR ' resource and use GetString() to retrieve the string from the resource fork. (Or a 'STR#' resource and GetIndString().) Personally, I keep to C strings as much as possible and use the ANSI library routines, using c2pstr() when necessary. For those few strings that should stay Pascal strings most of the time, I terminate the string variable name with a 'P' to remind myself. - Mark