[fpc-pascal]What exactly is Pchar good for ?

Nikolai Zhubr s001 at hotbox.ru
Fri Jul 19 01:22:03 CEST 2002


Hi,
Thursday, 18 July, 2002, 23:16:12, Martyn Ranyard wrote:
[...]
> This doesn't seem right - a PChar is just a pointer to a character, where the 

Consider the following example (for Delphi):

var
  st: string[10] = 'Hello';
begin
  writeln(PChar(@st[1]) + ' !!!');
end.

The output is:
Hello !!!
Does this mean we can add strings to pointers? No. Consider the
next one:

type
  HonestPChar = ^Char;
var
  st: string[10] = 'Hello';
begin
  writeln(HonestPChar(@st[1]) + ' !!!');
end.

Delphi says:
Error: Incompatible types: 'string' and 'HonestPChar'

BTW, this doesn't fit well enough in my head ;) yet. Its like C belch
or something... I'm afraid of pchars.

Sorry don't know how FPC takes this (yet).

> string follows on flat-form in memory, with a #0 at the end.  Is this what's
> going wrong?  Have you forgotton to add the null character at the end?
[...]

>  I would have thought that the code behind ansistrings are pchars, but I may
> be wrong.

Delphi's rtl handles strings' operations independently of pchars
whenever possible, iirc. (That is, when not mixed, etc.)
-- 
Best regards,
 Nikolai Zhubr






More information about the fpc-pascal mailing list