Recent

Author Topic: [Solved] Free Pascal equivalent of C const char *  (Read 2212 times)

zamronypj

  • Full Member
  • ***
  • Posts: 122
    • Fano Framework, Free Pascal web application framework
[Solved] Free Pascal equivalent of C const char *
« on: May 14, 2021, 08:20:06 am »
What is Pascal equivalent of

Code: [Select]
const char * str;
« Last Edit: May 14, 2021, 05:50:56 pm by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: Free Pascal equivalent of C const char *
« Reply #1 on: May 14, 2021, 08:49:15 am »
What is Pascal equivalent of
Code: [Select]
const char * str;

I think:
Code: Pascal  [Select][+][-]
  1. {$writeableconstants off}
  2. const str: PChar;
should be about the same.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2693
Re: Free Pascal equivalent of C const char *
« Reply #2 on: May 14, 2021, 08:53:45 am »
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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

zamronypj

  • Full Member
  • ***
  • Posts: 122
    • Fano Framework, Free Pascal web application framework
Re: Free Pascal equivalent of C const char *
« Reply #3 on: May 14, 2021, 09:02:48 am »
@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.
« Last Edit: May 14, 2021, 09:13:28 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

PascalDragon

  • Hero Member
  • *****
  • Posts: 3056
  • Compiler Developer
Re: Free Pascal equivalent of C const char *
« Reply #4 on: May 14, 2021, 09:17:16 am »
@MarkMLl in context of declaring variable. In C, you can declare mutable pointer with immutable content.

Pascal doesn't have the ability to decide whether the content should be immutable or not. The closest is the following:

Code: Pascal  [Select][+][-]
  1. program tstrtest;
  2.  
  3. var
  4.   p: PChar = 'Hello World';
  5. begin
  6.   p[0] := 'P';
  7. 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.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 930
    • Lebeau Software
Re: Free Pascal equivalent of C const char *
« Reply #5 on: May 14, 2021, 06:45:01 pm »
I think:
Code: Pascal  [Select][+][-]
  1. {$writeableconstants off}
  2. const str: PChar;
should be about the same.

That is not exactly the same.  "const char*" is a mutable pointer to immutable data, whereas "const PChar" is an immutable pointer to mutable data.

Also, PAnsiChar would technically be more correct, and safer under {$MODESWITCH UNICODESTRINGS}/{$Mode DelphiUnicode} where PChar is PWideChar instead.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: [Solved] Free Pascal equivalent of C const char *
« Reply #6 on: May 14, 2021, 07:40:51 pm »
Yeah, I had some misgivings, which is why I said "I think". I'm not much versed on C subtleties :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018