Recent

Author Topic: Operator prededence with @ and []  (Read 695 times)

ssawgift

  • New Member
  • *
  • Posts: 46
    • My Personal Website
Operator prededence with @ and []
« on: November 27, 2022, 06:35:10 am »
The doc page https://wiki.freepascal.org/Operator) does not give me clue on the index operator []. Given the following code:
Code: Pascal  [Select][+][-]
  1. var
  2.   s: string;
  3.  
  4. begin
  5. s := 'string';
  6.  
  7. {
  8. Does the compiler
  9. * take address S first and then add offset 2 to the pointer
  10. or
  11. * take address of 2nd char in string and then convert it to a pointer?
  12. }
  13. writeln(stricomp(@s[2], 'String'));
  14.  

Thaddy

  • Hero Member
  • *****
  • Posts: 12572
Re: Operator prededence with @ and []
« Reply #1 on: November 27, 2022, 08:44:42 am »
The compiler will translate it into a straight single pointer for Shortstring and AnsiString. But be careful:
'string' isn't always what you think it is. Which string type do you expect?
Also, things like stricomp are highly discouraged. (Usually bad coding practice) It assumes a zero terminated array of AnsiChar. (Pchar) It is usually better to manipulate strings'the Pascal way: in this case **s://www.freepascal.org/docs-html/rtl/sysutils/comparetext.html
And even then you may run into trouble if you are not aware which kind of string you are handling.
PChar? // and its siblings can cause mayhem:
  -- PAnsiChar?
  -- PWideChar? What is it!!!!
ShortString? // native in {$H-} mode
AnsiString? // native in {$H+} mode
WideString? // non - native but accepted
UTF8String? // non - native but accepted and hacked around by Lazarus
UnicodeString? // native UTF16 and in {$mode delphiunicode}

And then we have Rawbytestring, // basically single byte ansi, ...
« Last Edit: November 27, 2022, 09:13:39 am by Thaddy »
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

WooBean

  • Full Member
  • ***
  • Posts: 170
Re: Operator prededence with @ and []
« Reply #2 on: November 27, 2022, 09:46:50 am »
The short answer:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objFPC} {$H+}
  3. var s:string='abcdefg';
  4.     p:pointer;
  5. begin
  6.   writeln(pchar(@s[2])); //output: bcdefg
  7.   // so, @s[2] means address of s[2], no "operator" precedence involved
  8.   p:=@s[2];//@s[2] means address of s[2], here too
  9.   writeln(char(p^)); //output: b
  10.   readln;
  11. end.
[Edited]
As far as adding offsett to "s" address I think that it is added "1"  - not 2. I assumed size of string char = 1. (Pascal strings are not zero-counted but one-counted, first index is one).

Code: ASM  [Select][+][-]
  1. project1.lpr:8  p:=@s[2];
  2.      mov rax,[rip+$0000CB28]
  3.      add rax,$01
  4.      mov [rip+$000109ED],rax
  5.  
« Last Edit: November 27, 2022, 11:10:03 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

ASerge

  • Hero Member
  • *****
  • Posts: 2049
Re: Operator prededence with @ and []
« Reply #3 on: November 27, 2022, 10:38:14 am »
The doc page https://wiki.freepascal.org/Operator) does not give me clue on the index operator []. Given the following code:
In pascal, [] is not an operator, but a syntactic part (special symbols). It is used for indexing arrays and properties, designating sets and dynamic arrays. It is also used in declaration. Since this is a syntactic part, it is taken into account before any operators.

 

TinyPortal © 2005-2018