Difference between revisions of "@"

From Lazarus wiki
Jump to navigationJump to search
(more generic intro statement)
Line 3: Line 3:
 
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">@</div>
 
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">@</div>
  
The address operator <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> returns the address of a [[Variable|variable]], [[Procedure|<syntaxhighlight lang="pascal" enclose="none">procedure</syntaxhighlight>]] or [[Function|<syntaxhighlight lang="pascal" enclose="none">function</syntaxhighlight>]].
+
The address operator <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> returns the address of an [[Identifier|identifier]] that is associated with an address (usually a [[Variable|variable]] or [[Routine|routine]], but also a [[Label#assembler|label]]).
  
 
Normally, the value <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> returns is an ''untyped'' [[Pointer|<syntaxhighlight lang="pascal" enclose="none">pointer</syntaxhighlight>]].
 
Normally, the value <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> returns is an ''untyped'' [[Pointer|<syntaxhighlight lang="pascal" enclose="none">pointer</syntaxhighlight>]].

Revision as of 14:01, 21 April 2019

English (en) suomi (fi) français (fr) русский (ru)

@

The address operator @ returns the address of an identifier that is associated with an address (usually a variable or routine, but also a label).

Normally, the value @ returns is an untyped pointer. If you are handling pointers a lot, and want to mitigate issues with passing references of wrong type's target, you have use the directive {$typedaddress on}.

Here some example to demonstrate, what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result:

 1program untypedAddressDemo(input, output, stderr);
 2
 3procedure incrementIntByRef(const ref: PByte);
 4begin
 5	inc(ref^);
 6end;
 7
 8var
 9	foo: integer;
10begin
11	foo := -1;
12	incrementIntByRef(@foo);
13	writeLn(foo);
14end.

It was intended, that 0 (zero) gets printed, but the program prints -256 instead. With {$typedaddress on} compilation fails with an incompatible type error. You usually want the latter behavior (compile-time failure) instead of wasting time with hours of debugging.

other remarks

  • In ASCII the character @ (AT sign): has the value 64.

read more


navigation bar: topic: Pascal symbols
single characters

+ (plus)  •  - (minus)  •  * (asterisk)  •  / (slash)
= (equal)  •  > (greater than)  •  < (less than)
. (period)  •  : (colon)  •  ; (semi colon)
^ (hat)  •  @ (at)
$ (dollar sign)  •  & (ampersand)  •  # (hash)
' (single quote)

character pairs

<> (not equal)  •  <= (less than or equal)  •  := (becomes)  •  >= (greater than or equal)

 •  >< (symmetric difference)  •  // (double slash)