Pointers and references: Difference between revisions
Content added Content deleted
(Split some 'C++' examples off into a new 'C' section, to emphasise the fact that this 'C' code is still supported by C++) |
No edit summary |
||
Line 260: | Line 260: | ||
my $arrayref = ['an', 'array']; |
my $arrayref = ['an', 'array']; |
||
my $hashref = { firstkey => 'a', secondkey => 'hash' } |
my $hashref = { firstkey => 'a', secondkey => 'hash' } |
||
==[[Pop11]]== |
|||
[[Category:Pop11]] |
|||
Pop11 uses reference model, conceptually all Pop11 data consists |
|||
of references. Therefore, normally there is no need for explicit |
|||
references: |
|||
vars vec1, vec2; |
|||
;;; Create a vector and assign (reference to) it to vec1 |
|||
consvector("a", "b", "c", 3) -> vec1; |
|||
;;; Copy (reference to) vector |
|||
vec1 -> vec2; |
|||
;;; Print value of vec1 |
|||
vec1 => |
|||
;;; Change first element of vec2 |
|||
"d" -> vec2(1); |
|||
;;; Print value of vec1 -- the value changes because vec1 and |
|||
;;; vec2 reference the same vector |
|||
vec1 => |
|||
However, if one needs extra indirection one can get it: |
|||
vars ref1, ref2; |
|||
;;; Allocate a reference to number 42 |
|||
consref(42) -> ref1; |
|||
;;; Copy reference |
|||
ref1 -> ref2; |
|||
;;; print value |
|||
cont(ref2) => |
|||
;;; Change referenced value |
|||
17 -> cont(ref2); |
|||
;;; print value of first reference |
|||
cont(ref1) => |
|||
Standard Pop11 does not allow pointer arithmetics or address operator. |
|||
There is a low-level extension (Syspop11) which has C-like pointers |
|||
(with pointer arithmetics), but those does not count as "basic data |
|||
operation". |
|||
==[[Tcl]]== |
==[[Tcl]]== |