[comp.sys.mac.programmer] Need help in THINK C

)) (10/25/89)

In article <573@telesoft.com> garym@telesoft.com (Gary Morris @nova) writes:

>Not only do you need to add a NUL (0) at the end, you must skip over the
>length byte at the beginning of Pascal strings.  If you have a Pascal string
>Pstr, you can pass it to a C routine, like so:

>	Pstr[ Pstr[0] ] = (char) 0;	/* add the NUL on the end */

>	strcpy( DestStr, &Pstr[1] );	/* copy string with first character */

Of course this is completelky, utterly wrong. It zaps the last character
in the string to a 0 ! WHat you want is

Pstr[ Pstr[0] +1] = (char) 0;

This has the additional disadvantage of requiring the pascal string to
have a length < 255 characters, as you will otherwise overwrite something
else.

>Or if you don't want to modify the source string by adding a NUL on the end
>you can use strncpy and specify the length to copy:

>	strncpy( DestStr, &Pstr[1], (int) Pstr[0] );

You'd want to add a null at the end, too, so define the following
macro:

#define p_str_to_c(_p_, _c_) \
strncpy(_c_, &((_p_)[1]), (int) (_p_)[0]); \
(_c_)[(_p_)[0]] = 0;

>Or with Think C you can use the supplied routine "PtoCstr" to convert a 
>string from Pascal format to C format.

>Disclaimer: I haven't actually tried these examples and I'm new to using C
>on the Mac (I prefer TeleSoft Ada).  If these examples don't work I will

As indeed they don't. Yo, the group's called comp.sys.mac.programmer,
not comp.sys.mac.programming, and therefore your post got to comp.sys.mac.
Follow-ups redirtected to comp.sys.mac.programmer

h+@nada.kth.se == h+@proxxi.se

-- 
Bango. To the BatMobile! -Let's go!