Richard Engebretson schrieb:
> Freepascal follows the turbo-pascal string type convention.
That depends on the mode and on the switch {$H+/-}.
Objpas and Delphi modes default to $H+, the other modes default to $H-.
In mode {$H-}, Strings are TP compatible. So I assume you are talking
about this mode.
> First, the string data type is created at compile time. Unlike C
> pointers created dynamically.
>
> Second, a Pascal string is an indexed array. So a character can be
> addressed like S[3]. A length byte is at S[0].
Right.
> Third, string variables are passed to procedures as pointers.
That depends on how you declare the procedure!
a) procedure foo(s: String);
b) procedure foo(const s: String);
c) procedure foo(var s: String);
In b and c, a reference is used. In a, the string is copied to the stack.
> To create C strings (null terminated pointers to a sequence of
> characters) one might do the following;
You simply use StrPCopy.
> Turbo-Strings are different with some good reasons. Not necessarily a
> fault.
The main problem is that they have a maximum length of 255 characters.
That's one reason why I prefer AnsiString/{$H+}.
Some things that happen with AnsiStrings:
- s1:=s2 is just a pointer copy and reference counter change. The whole
data is copied only if you modify one of the strings.
- you can easily pass AnsiStrings to a procedure which expects a PChar:
simply use a cast: foo(PChar(str))
- s[0] is not the length (is is an error), get/set the length with
length() and setlength()
Wolf


|