[fpc-pascal] more questions on storage

Michael Van Canneyt michael at freepascal.org
Thu Sep 27 14:00:16 CEST 2007



On Thu, 27 Sep 2007, Marco van de Voort wrote:

> > marcov at stack.nl (Marco van de Voort) wrote:
> > > A cast is really a cast. IOW the cast pchar(ansistring) is mostly a
> > > no-op. Traditional C code then usually treats the #0 as end of
> > > string. 
> > 
> > PChar(AnsiString) was a no-op typecast in the past and is nowadays a
> > function. It checks whether the AnsiString is nil and if yes returns a
> > pointer to a string containing one character: #0.
> > 
> > That means:
> > Pointer(AnsiString) <> Pointer(PChar(AnsiString))
> > 
> > To get a no-op typecase you can use:
> > MyPChar:=PChar(Pointer(AnsiString));
> 
> Is this delphi compat, and if yes, what is this exactly for?

It is Delphi compatible.

PChar(AnsiString)

Will always point to a null-terminated string. If the string is empty
(which means the pointer value is Nil), a pointer to a pre-defined 
null-terminated string is returned. In other words: you always get a non-nil
value.

On the other hand:

Pointer(AnsiString)

Returns Nil if the string is empty.

Michael.



More information about the fpc-pascal mailing list