Search code examples
delphiexceptionpchar

delphi modify pchar at runtime


I need to modify pchar string at runtime. Help me with this code:

var
  s:pChar;
begin
  s:='123123';
  s[0]:=#32; // SO HERE I HAVE EXCEPTION !!!
end.

Now i have exception in Delphi 7 ! My project is not using native pascal strings (no any windows.pas classes and others)


Solution

  • You can:

    procedure StrCopy(destination, source: PChar);
    begin
      // Iterate source until you find #0
      // and copy all characters to destination.
      // Remember to allocate proper amount of memory
      // (length of source string and a null terminator)
      // for destination before StrCopy() call 
    end;
    
    var
      str: array[0..9] of Char;
    begin
      StrCopy(str, '123123');
      s[0]:=#32;
    end.