Forum > Beginners

How to prevent automate assign of PChar to String

(1/4) > >>

Okoba:
For now, FPC automatically set a PChar to a String value and I want to disallow it for safety. Is there a way?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; var  A: PChar;  B: String;begin  A := GetMem(3);  A[0] := 'x';  A[1] := 'y';  A[2] := #0;  B := A; //Assign automatically  WriteLn(B);  ReadLn;end.                                          

KodeZwerg:

--- Quote from: Okoba on February 23, 2024, 09:19:30 pm ---For now, FPC automatically set a PChar to a String value and I want to disallow it for safety. Is there a way?
--- End quote ---
It is not FPC, it is you that command "B := A;"

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{$APPTYPE CONSOLE}var  ps: PChar;  s: string;begin  s := 'FPC does not automagical assign anything to me.';  ps := 'yx';  WriteLn(ps);  WriteLn(s);  ReadLn;end.

Okoba:
I was not clear. I dont want FPC to let me write that code. I want to get an error. Like when you want to assign an integer to a string and compiler raises an error.

Thaddy:
Ignore the error, because it is parsed ...

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{$H+}{$macro on}{$Define string:=}var  A: PChar;  B: String;begin  A := GetMem(3);  A[0] := 'x';  A[1] := 'y';  A[2] := #0;  B := A; //Assign automatically  WriteLn(B);  ReadLn;end.         

And it is the world upside down: Pascal strings are safe, Pchars are not....       
And please call freemem.....                         

KodeZwerg:

--- Quote from: Okoba on February 23, 2024, 09:35:02 pm ---I was not clear. I dont want FPC to let me write that code. I want to get an error. Like when you want to assign an integer to a string and compiler raises an error.
--- End quote ---
Beside killing a type like Thaddy I guess showed, no. That is part of FPC design.

Navigation

[0] Message Index

[#] Next page

Go to full version