Forum > General
[Solved] Free Pascal equivalent of C const char *
zamronypj:
What is Pascal equivalent of
--- Code: ---const char * str;
--- End code ---
lucamar:
--- Quote from: zamronypj on May 14, 2021, 08:20:06 am ---What is Pascal equivalent of
--- Code: ---const char * str;
--- End code ---
--- End quote ---
I think:
--- 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";}};} ---{$writeableconstants off}const str: PChar;should be about the same.
MarkMLl:
Is this in the context of declaring a variable, in which case do you want a C-type or Pascal-type string, or a parameter?
MarkMLl
zamronypj:
@MarkMLl in context of declaring variable. To be more specific, I have C struct with member of type const char *. I want to know how to accomplish similar semantic with Free Pascal record.
In C, you can declare mutable pointer with immutable content.
PascalDragon:
--- Quote from: zamronypj on May 14, 2021, 09:02:48 am ---@MarkMLl in context of declaring variable. In C, you can declare mutable pointer with immutable content.
--- End quote ---
Pascal doesn't have the ability to decide whether the content should be immutable or not. The closest is the following:
--- 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 tstrtest; var p: PChar = 'Hello World';begin p[0] := 'P';end.
Writing to the content of p will trigger an exception, because the 'Hello World' string data will reside in a read only section (if the OS as well as the assembler support it). However as soon as you change p to something that is dynamically allocated there won't be any exception.
Declaring p as a const instead of a var together with $WriteableConsts Off as lucamar showed will lead to a p variable that cannot be changed.
Navigation
[0] Message Index
[#] Next page