Board index » delphi » Doubt on passing strings
squai...@yahoo.com (Steven Quail)
![]() Delphi Developer |
Tue, 17 Feb 2004 00:50:37 GMT
|
squai...@yahoo.com (Steven Quail)
![]() Delphi Developer |
Tue, 17 Feb 2004 00:50:37 GMT
Doubt on passing strings
Hi to all,
I think I finally have got the hang of references, however If I have the call> Var setLength(Str, 100); and I get the compile time error: Can anyone please explain why I get this. Is it because String points to a fixed position in Thanks Steven Quail. |
David Reev
![]() Delphi Developer |
Tue, 17 Feb 2004 08:07:23 GMT
Re:Doubt on passing stringsQuoteSteven Quail <squai...@yahoo.com> wrote in message Quote> Hi to all, the types must match exactly. How about something like.... Var Str : String; SetLength(Str, 100); Procedure TestProc(Ptr : PChar); Note that it is necessary to explicitly set the length of Str to match the Dave |
J Fren
![]() Delphi Developer |
Tue, 17 Feb 2004 20:30:28 GMT
Re:Doubt on passing stringsHere you are passing a 'conversion' of your string - this is effectively unmodifyable. TestProc(PChar(Str)); and here you are expecting the address of a PChar that you *can* Procedure TestProc(var StrPass : PChar) using @Str[1] will give you a PChar type pointer. IMO - and this will probably be disagreed with - PChars are pretty The whole concept of an ASCIIZ string is .. well not very sound. On 30 Aug 2001 09:50:37 -0700, squai...@yahoo.com (Steven Quail) Quote>Hi to all, |
David C. Ullri
![]() Delphi Developer |
Tue, 17 Feb 2004 22:09:32 GMT
Re:Doubt on passing stringsOn 30 Aug 2001 09:50:37 -0700, squai...@yahoo.com (Steven Quail) wrote: Quote>Hi to all, to pass a _variable_ to the routine. You're passing PChar(Str), which is not a variable. Two solutions: (i) It's very unlikely that you Two examples - they both work: procedure TestStringProc(var s: string); procedure TestPCharProc(P: PChar); //note those are both BUGGY routines, included procedure TForm1.Button1Click(Sender: TObject); Quote>Is it because String points to a fixed position in |
VBDi
![]() Delphi Developer |
Wed, 18 Feb 2004 06:18:23 GMT
Re:Doubt on passing stringsIm Artikel <3aabc57a.0108300850.4233b...@posting.google.com>, squai...@yahoo.com (Steven Quail) schreibt: Quote>Is it because String points to a fixed position in Quote>Var Quote> setLength(Str, 100); Quote> TestProc(tempstr); //(PChar(Str)); parameter, and all works fine. You need "var" only, when the modified pointer is of importance in the calling subroutine. If this really is your intention, then you must pass an alterable pointer to the subroutine, like "tempstr" above. IOW, you need "var" only, when a data object must be moved in memory, and the DoDi |
David C. Ullri
![]() Delphi Developer |
Wed, 18 Feb 2004 21:10:15 GMT
Re:Doubt on passing stringsOn 31 Aug 2001 22:18:23 GMT, vb...@aol.com (VBDis) wrote: Quote>Im Artikel <3aabc57a.0108300850.4233b...@posting.google.com>, if TestProc is as in his later posts then he really does need a var parameter, he really is modifying the value of a PChar (incrementing it by some amount.) Not that that's necessarily the best way to be doing whatever he's Quote> You need "var" only, when the modified pointer |