On Feb 18, 7:01 am, Wolf Behrenhoff
<NoSpamPleaseButThisIsVal...@[EMAIL PROTECTED]
> wrote:
> 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
Thank you.
I have looked hard at AnsiStrings (not the source). They try to bridge
the turbo with delphi pascal. But I am avoiding the delphi mode. The
delphi mode hides pointers and has other secrets that confuse me.
My basic point is pascal has some very nice string types. Pascal is
NOT like C and that is good.
My only disagreement is AFAIK the string is not copied to the stack in
the first a) case, as one would expect. But is passed by reference.
However, this is very unclear to me.
I have been using my little Cstring function with ncurses and it
works, but I would prefer the StrPCopy. Thanks for the excellent
advice.
Rick.


|