Strings in C (Re: ambiguous ?)
Joe Brownlee
joe at gistdev.UUCP
Sun Oct 29 07:16:27 AEST 1989
In lots of articles, lots of people say lots of things about how Macintosh
C compilers handle counted (or Pascal-style, if you will) strings.
In an article someone says (sorry, lost the attribution):
>Until we get a real Mac C type with the balls to post in this group...
Well, judge for yourself :-). I just hack a bit as well, but I happen to have
the THINK C 4.0 Manual handy, so here goes. By the way, THINK C 4.0 is
advertised as being very ANSI conformant, and for the most part, it is. The
libraries seem to be the most conformant feature. However, this version does
not support "const", "volatile", or "signed", for example.
First, string literals which begin with a "\p" are considered type "Str255",
the type of string used by Pascal and the Macintosh ROM toolbox. Type Str255
is defined in the header file "MacTypes.h" like so:
typedef unsigned char Str255[256];
The count is kept in the first byte.
Routines are provided to convert between the two formats of strings. However,
in my experience, it is better to write parallel versions of the standard "str"
routines which operate on counted strings, since all toolbox calls operate on
them. Thus you would say:
Str255 s;
[...]
(void)Pstrcpy( s, "\pThis is how to set a counted string." );
Appendix A of the "Standard Libraries Reference" Manual (which deals with
printf() and scanf()) says the following in its table of format characters:
Character Argument Type Output
p void * An eight-digit hexadecimal number.
s char * A string. [...] Prints a C-style string (an object
of type char *). It prints the string until one of
the following happens:
. It encounters a NULL character, which it will
not print.
. It prints the maximum number of characters
allowed by the precision directive.
With the # flag, this specifier prints a Pascal-
style string (an object of type Str255).
In other words:
Str255 s;
[...]
(void)printf( "This is a Pascal string: %#s\n", s );
...would be used to display the contents of a counted string.
As stated earlier, I have never taken a Mac program to UNIX or DOS, but I have
taken UNIX and DOS programs to the Mac, so I do not find the above to be a
problem.
Disclaimer: I am not associated with Symantec/THINK C in any other way than as
a user. I do necessarily endorse the above as being ANSI conformant or even as
an allowed extension. Others in this group can speak to this issue (please,
do). I am simply posting this information so that any futher discussion
can be informed rather than "hear-say".
Joe Brownlee | The best diplomat I know is a fully activated
Global Information Systems | phaser bank. -- Montgomery Scott
1800 Woodfield Drive |
Savoy, Illinois 61874 | Pay attention to what I say. Start a trend.
(217) 352-1165 | UUCP: {uunet,pur-ee,convex}!gistdev!joe
More information about the Comp.lang.c
mailing list