0

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)

2 Answers2

1

String literals are read only and cannot be modified. Hence the runtime error. You'll need to use a variable.

var
  S: array[0..6] of Char;
....
// Populate S with your own library function
S[0] := #32;

Since you aren't using the Delphi runtime library you'll need to come up with your own functions to populate character arrays. For instance, you can write your own StrLen, StrCopy etc. You'll want to make versions that are passed destination buffer lengths to ensure that you don't overrun said buffers.

Of course, not using the built in string type will be inconvenient. You might need to come up with something a little more powerful than ad hoc character arrays.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

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.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
g2mk
  • 509
  • 3
  • 13
  • You can pass str directly to StrCopy in the third block. Asker isn't using libraries though so can't use any of the first three blocks of code. The final block is the same as in the question. Fails with runtime error for the reasons I explained in my answer. – David Heffernan Sep 07 '15 at 17:06
  • 1 example will not work because no getMem and memoryManagement. 2,3 because no strCopy. 4 sent me to exception :( – Vadim Kaplan Sep 07 '15 at 17:42
  • Argh... Sorry. If you have to write you code without any standard modules and without use of pascal strings you should implement you own StrCopy (as D. H. have written). Isn't it a kind of homework or something?!? – g2mk Sep 07 '15 at 18:30
  • @DavidHeffernan: You are right - it's not good to give answers on SO without right compiler (I have checked my code under FPC) and when you are doing few other things at the time... – g2mk Sep 07 '15 at 18:39
  • I edited the code to simplify it. This is pretty much what I said, although you don't explain why the code in the question fails. – David Heffernan Sep 07 '15 at 19:01
  • Original of ASM function StrCopy(Dest: PChar; const Source: PChar): PChar; did not work (same exception occured)... – Vadim Kaplan Sep 07 '15 at 19:59
  • @VadimKaplan I'm not sure you are really grasping this. Your `asm` function doesn't work? We can't see it. I guess you made a mistake. Why are you writing `asm` anyway? Start with something a bit simpler. It's quite easy to write a `StrCopy` function. If you don't know how to, then you need to develop some more skills before proceeding. You'll need a lot more mojo if you are going to plug on without an RTL. – David Heffernan Sep 07 '15 at 20:36
  • @David, sorry All work fine, i really make mistake. I took asm function from d7 sources SysUtils.pas . About mojo - now i in progress of earning mojo. at least I think so :) Thank you David ! – Vadim Kaplan Sep 08 '15 at 10:05
  • Always start by writing Pascal. If perf matters, consider translating to asm. – David Heffernan Sep 08 '15 at 10:07