^: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: ^
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ^ | |
| 2 | + | ||
| 3 | + | In [[ASCII]] the character code decimal `94` (or [[Hexadecimal|hexadecimal]] `5E`) is defined to be `^` (circumflex accent). | |
| 4 | + | ||
| 5 | + | For any data type, a [[Pointer|pointer type]] for that data can be declared using the operator `^` in front of the [[Data type|data type]]. | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | program pointerDemo(input, output, stderr); | |
| 9 | + | ||
| 10 | + | type | |
| 11 | + | listItem = record | |
| 12 | + | payload: integer; | |
| 13 | + | // next _points_ _to_ a list item | |
| 14 | + | next: ^listItem; | |
| 15 | + | end; | |
| 16 | + | ||
| 17 | + | var | |
| 18 | + | start: ^listItem; | |
| 19 | + | ||
| 20 | + | begin | |
| 21 | + | new(start); | |
| 22 | + | if not assigned(start) then | |
| 23 | + | begin | |
| 24 | + | writeLn(stderr, 'obtaining memory for start failed'); | |
| 25 | + | halt(1); | |
| 26 | + | end; | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | A `pointer` can be followed, *dereferenced* by appending a `^` 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 `pointer`, operations and syntax for that type are valid, e.g. [[Becomes|assignment]] in the following example. | |
| 30 | + | ||
| 31 | + | ``` pascal | |
| 32 | + | // _de-reference_ the pointer, i.e. follow it | |
| 33 | + | start^.payload := 7; | |
| 34 | + | ||
| 35 | + | dispose(start); | |
| 36 | + | end. | |
| 37 | + | ``` | |
| 38 | + | ||
| 39 | + | However, attempting to follow a [[Nil|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. | |
| 40 | + | ||
| 41 | + | ## see also | |
| 42 | + | ||
| 43 | + | - [[$pointerMath|{$pointerMath off}]] disallows usage of pointers in arithmetic expressions | |
| 44 | + | - [[$typeAaddress|{$typedAddress on}]] in conjunction with the [[@|@-address-operator]] | |
| 45 | + | - [[sModeswitch|{$modeSwitch autoDeref-}]] | |
| 46 | + | ||
| 47 | + | --- | |
| 48 | + | *From the Free Pascal wiki, page "^", as it stood on 2023-02-18 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
