Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!rochester!pt.cs.cmu.edu!henry.ECE.CMU.EDU!hairston From: hairston@henry.ECE.CMU.EDU (David Hairston) Newsgroups: comp.sys.mac.programmer Subject: Re: Str255 and char* Keywords: THINK C 4.0 Message-ID: <11689@pt.cs.cmu.edu> Date: 27 Jan 91 20:35:17 GMT References: <1991Jan27.181816.22644@sbcs.sunysb.edu> Distribution: usa Organization: Gaia II Lines: 50 [mmoss@csws9.ic.sunysb.edu (Matthew D Moss) writes:] [] Can someone tell me what exactly is the difference between [] >Str255 and char* ? [] [] Looking through source files, I see Str255 defined as an [] array. Is that the same as a char pointer? the type definition of Str255 in the THINK C enviroment is: typedef unsigned char Str255[256]; makes sense considering the length byte which preceeds pascal strings. when using Str255's in situations that require (char *)'s you need to make a cast because of the difference in underlying type (char vs unsigned char) and consider the syntax of the string (i.e. a pascal string is preceded by an unsigned length byte and a c string is terminated by a null byte). there are tools for handling these conversions. Str255 is defined as an array which does allocate storage (i.e. 256 unsigned chars at a fixed address). a pointer does not allocate storage for the object pointed at, just storage for itself (i.e. a long word). [] And, if I were to use char*, do I have to allocate space [] before I define the string? depends ... ;) char *cp = "address of string constant"; is okay if you simply want to use a fixed string. the following is also okay (i haven't considered pascal <=> c issues): int some_flag; char cbuf1[256], cbuf2[256], *cp; ... strcpy(cbuf1, "a given string value"); strcpy(cbuf2, "another string value"); cp = (some_flag ? cbuf1 : cbuf2); in this case, changing the pointer doesn't lose information as it would in the first example. there are other reasons for using buffers (arrays) also. -dave- hairston@henry.ece.cmu.edu