StrPas question

From: Ian Hinson (pparagon_at_bigpond.net.au)
Date: 12/15/04


Date: Wed, 15 Dec 2004 17:26:42 GMT

I kind of baffled why the StrPas function is so simple.
The SysUtils source for the StrPas function says:

function StrPas(const Str: PChar): string;
begin
   Result := Str;
end;

I would have expected StrPas to make a *copy* of the source PChar so that
the program can then do with the source string whatever it wants after that.
Therefore, shouldn't the StrPas function instead look like this?

function StrPas(const Str: PChar): string;
begin
   SetString(Result, Str, StrLen(Str));
end;

Maybe I'm just missing something in my understanding of Strings.
Or maybe the const parameter is achieving an extra step that I've
overlooked.
Could someone please clarify?

Thanks,
Ian.



Relevant Pages

  • Re: StrPas question
    ... > I kind of baffled why the StrPas function is so simple. ... > the program can then do with the source string whatever it wants after that. ... That's what already happens when you assign a PChar to a string. ... calling SetString, then you can just as well do the assignment directly. ...
    (alt.comp.lang.borland-delphi)
  • Re: common coding mistakes
    ... function GetStr: PChar; ... Str: String; ... Jonathan ...
    (borland.public.delphi.non-technical)