Board index » delphi » D1: PChar and zero-based Char-arrays

D1: PChar and zero-based Char-arrays

Hi everybody,

Im still not quite able to cope with 'PChar' and zero-based character
arrays. I always thought they were the same. Maybe somebody can give me
the final clue.

First example:

sstring : String;
pstring : Array[0..4] of char;

sstring := 'abcd';
strpcopy(pstring,sstring);   This gives me pstring = ('a','b','c','d',#0)

Second example:

sstring : String;
pstring : PChar;

sstring := 'abcd';
GetMem(pstring,length(sstring)+1);
strpcopy(pstring,sstring);          This gives me pstring = ('abcd')
FreeMem(pstring,length(sstring)+1);

Now my questions: Why do I get a different result ?

                  Is a variable of type PChar a zero-based character
                  array or a pointer to a zero-based character array ?

Thanks for any help !!

Barbara.

 

Re:D1: PChar and zero-based Char-arrays


In article <56s2bb$...@news00.btx.dtag.de>,
   LPP.BK...@t-online.de (B. Keil) wrote:
]-Hi everybody,
]-
]-Im still not quite able to cope with 'PChar' and zero-based character
]-arrays. I always thought they were the same. Maybe somebody can give me
]-the final clue.
]-
]-First example:
]-
]-sstring : String;
]-pstring : Array[0..4] of char;
]-
]-sstring := 'abcd';
]-strpcopy(pstring,sstring);   This gives me pstring = ('a','b','c','d',#0)
]-
]-
]-
]-Second example:
]-
]-sstring : String;
]-pstring : PChar;
]-
]-sstring := 'abcd';
]-GetMem(pstring,length(sstring)+1);
]-strpcopy(pstring,sstring);          This gives me pstring = ('abcd')
]-FreeMem(pstring,length(sstring)+1);
]-
]-
]-Now my questions: Why do I get a different result ?
]-
]-                  Is a variable of type PChar a zero-based character
]-                  array or a pointer to a zero-based character array ?
]-
]-Thanks for any help !!
]-
]-Barbara.

check Chapter 15 of the Object Pascal Language Guide.  it describes
the exact relation between character pointers (PChars) and zero-based
character arrays.  if you have the 1.02 CD then the OPLG (in Adobe
Acrobat format) is somewhere on your disc...if not then you can down
load a copy from Borland's web site or the Delphi Super Page.

I'd guess that the "different" results you see are due to the way the
de{*word*81} displays the contents of different data types.  If you try
the program below you'll find that the output of the three writeln
statements is identical

Mark Vaughan

----

uses WinCRT, SysUtils;

Var
  sString : String;
  aString : Array[0..4] of char;
  pstring : PChar;

BEGIN
  sstring := 'abcd';
  strpcopy(aString, sString);
  GetMem(pString, length(sstring)+1);
  strpcopy(pString,sString);

  writeln('sString => ', sString);
  writeln('aString => ', aString);
  writeln('pString => ', pString);

  FreeMem(pstring,length(sstring)+1);
END.

Re:D1: PChar and zero-based Char-arrays


m.>]-sstring := 'abcd';
m.>]-strpcopy(pstring,sstring);   This gives me pstring = ('a','b','c','d',#0
m.>]-strpcopy(pstring,sstring);          This gives me pstring = ('abcd')

If you'd like to avoid the ('a', 'b', 'c') difficult to read thingy in the
watch window, just change 'arrvar' in the watches window to 'pchar(arrvar)'.

--
Mr.Hillbilly (eiv...@infolink.no) http://www.sn.no/~eivind
 tHyperSoft HyperReader 1.0t

Other Threads