[comp.sys.mac.programmer] Str255 and char*

mmoss@csws9.ic.sunysb.edu (Matthew D Moss) (01/28/91)

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?

And, if I were to use char*, do I have to allocate space
before I define the string?

(Sorry if this has been asked before :)


Thanks,

Matthew Moss		(mmoss@libserv1.ic.sunysb.edu)

hairston@henry.ECE.CMU.EDU (David Hairston) (01/28/91)

[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

Lawson.English@p88.f15.n300.z1.fidonet.org (Lawson English) (02/01/91)

Matthew D Moss writes in a message to All

MDM> Can someone tell me what exactly is the difference between Str255 
MDM> and char* 

"char*" is a pointer to an arbitrary number of bytes. Str255 is an array of
256 bytes with a length byte at the beginning (hence Str255). You can use a
Str255 as a char* with typecasting, but you must remember to begin the reference
to the char-data at myStr255[1]: "*((char *)myStr255+1)" or somesuch (allowing
for bad parenthesis usage).

Lawson
 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!300!15.88!Lawson.English
Internet: Lawson.English@p88.f15.n300.z1.fidonet.org