Difference between revisions of "^"
(Created page with "<div style="float:left; margin: 0 25px 20px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">^</div> In ASC...") |
m (unify page target) |
||
(16 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{^}} | ||
<div style="float:left; margin: 0 25px 20px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">^</div> | <div style="float:left; margin: 0 25px 20px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">^</div> | ||
+ | In [[ASCII]] the character code decimal <syntaxhighlight lang="pascal" inline>94</syntaxhighlight> (or [[Hexadecimal|hexadecimal]] <syntaxhighlight lang="pascal" inline>5E</syntaxhighlight>) is defined to be <syntaxhighlight lang="pascal" inline>^</syntaxhighlight> (circumflex accent). | ||
+ | For any data type, a [[Pointer|<syntaxhighlight lang="pascal" inline>pointer</syntaxhighlight> type]] for that data can be declared using the operator <syntaxhighlight lang="pascal" inline>^</syntaxhighlight> in front of the [[Data type|data type]]. | ||
+ | <br clear="all"/> | ||
+ | <syntaxhighlight lang="pascal" line highlight="6-7,11"> | ||
+ | program pointerDemo(input, output, stderr); | ||
+ | type | ||
+ | listItem = record | ||
+ | payload: integer; | ||
+ | // next _points_ _to_ a list item | ||
+ | next: ^listItem; | ||
+ | end; | ||
− | + | var | |
+ | start: ^listItem; | ||
− | + | begin | |
+ | new(start); | ||
+ | if not assigned(start) then | ||
+ | begin | ||
+ | writeLn(stderr, 'obtaining memory for start failed'); | ||
+ | halt(1); | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | A <syntaxhighlight lang="pascal" inline>pointer</syntaxhighlight> can be followed, ''dereferenced'' by appending a <syntaxhighlight lang="pascal" inline>^</syntaxhighlight> to the [[Identifier|identifier]]. | ||
+ | Instead of having the memory address in your hands, you will look at the memory content ''at'' that address. | ||
+ | If it is a typed <syntaxhighlight lang="pascal" inline>pointer</syntaxhighlight>, operations and syntax for that type are valid, e.g. [[Becomes|assignment]] in the following example. | ||
+ | |||
+ | <syntaxhighlight lang="pascal" line start="21" highlight="1-2"> | ||
+ | // _de-reference_ the pointer, i.e. follow it | ||
+ | start^.payload := 7; | ||
+ | |||
+ | dispose(start); | ||
+ | end.</syntaxhighlight> | ||
+ | |||
+ | However, attempting to follow a [[Nil|<syntaxhighlight lang="pascal" inline>nil</syntaxhighlight>]] pointer will cause a [[runtime error|runtime error]] (RTE 216 “general protection fault”). | ||
+ | That is the situation the condition <syntaxhighlight lang="pascal" inline>not assigned(start)</syntaxhighlight> in line 15 is supposed to catch. | ||
+ | |||
+ | == see also == | ||
+ | * [[$pointerMath|<syntaxhighlight lang="pascal" inline>{$pointerMath off}</syntaxhighlight>]] disallows usage of pointers in arithmetic expressions | ||
+ | * [[$typeAaddress|<syntaxhighlight lang="pascal" inline>{$typedAddress on}</syntaxhighlight>]] in conjunction with the [[@|<syntaxhighlight lang="pascal" inline>@</syntaxhighlight>-address-operator]] | ||
+ | * [[sModeswitch|<syntaxhighlight lang="pascal" inline>{$modeSwitch autoDeref-}</syntaxhighlight>]] | ||
+ | |||
+ | {{Symbols}} |
Latest revision as of 02:19, 18 February 2023
│
English (en) │
suomi (fi) │
русский (ru) │
In ASCII the character code decimal 94
(or hexadecimal 5E
) is defined to be ^
(circumflex accent).
For any data type, a pointer
type for that data can be declared using the operator ^
in front of the data type.
1program pointerDemo(input, output, stderr);
2
3type
4 listItem = record
5 payload: integer;
6 // next _points_ _to_ a list item
7 next: ^listItem;
8 end;
9
10var
11 start: ^listItem;
12
13begin
14 new(start);
15 if not assigned(start) then
16 begin
17 writeLn(stderr, 'obtaining memory for start failed');
18 halt(1);
19 end;
A pointer
can be followed, dereferenced by appending a ^
to the identifier.
Instead of having the memory address in your hands, you will look at the memory content at that address.
If it is a typed pointer
, operations and syntax for that type are valid, e.g. assignment in the following example.
21 // _de-reference_ the pointer, i.e. follow it
22 start^.payload := 7;
23
24 dispose(start);
25end.
However, attempting to follow a nil
pointer will cause a runtime error (RTE 216 “general protection fault”).
That is the situation the condition not assigned(start)
in line 15 is supposed to catch.
see also
{$pointerMath off}
disallows usage of pointers in arithmetic expressions{$typedAddress on}
in conjunction with the@
-address-operator{$modeSwitch autoDeref-}
single characters |
|
character pairs |
|