Address of a variable: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Oberon-2}}: Added syntax highlighting)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(3 intermediate revisions by 3 users not shown)
Line 526:
 
===Get Address===
<syntaxhighlight lang="cobol">data division DATA DIVISION.
WORKING-STORAGE SECTION.
working-storage section.
01 ptr usageUSAGE pointerPOINTER.
01 var picPIC xX(64).
 
PROCEDURE DIVISION.
procedure division.
set SET ptr toTO addressADDRESS ofOF var.</syntaxhighlight>
 
===Set Address===
Line 538:
<syntaxhighlight lang="cobol">
OCOBOL*> Rosetta Code set address example
*> tectonics: cobc -x setaddr.cob && ./setaddr
program-id.IDENTIFICATION setaddrDIVISION.
dataPROGRAM-ID. divisionsetaddr.
working-storage section.
DATA DIVISION.
01 prealloc pic x(8) value 'somedata'.
WORKING-STORAGE SECTION.
01 var pic x(8) based.
01 prealloc PIC pic xX(8) valueVALUE 'somedata'.
procedure division.
set01 var address of var to addressPIC ofX(8) preallocBASED.
display var end-display
gobackPROCEDURE DIVISION.
SET ADDRESS OF var TO ADDRESS OF prealloc
end program setaddr.</syntaxhighlight>
DISPLAY var END-DISPLAY
*> 'somedata'
GOBACK.
END PROGRAM setaddr.
</syntaxhighlight>
 
=={{header|Commodore BASIC}}==
Line 1,780 ⟶ 1,786:
 
In addition some folks have written binary Python modules which implement "peek" and "poke" operations, but these are non-standard.
 
=={{header|Quackery}}==
 
Quackery does not have variables, and its memory model is based on dynamic arrays ("nests" in Quackery parlance) rather than a single continuous block of RAM, so, for example, an entry on the return stack has two parts; a pointer to a nest and a numerical offset into the nest (i.e. the location of an item within the nest) If the offset is non-negative, the offset is from the (zero-based) start of the nest, and if the offset is negative it is from the end of the nest, with the last item in the nest being at position -1.
 
The word <code>peek</code> returns a item located within a nest, given a nest and an offset.
 
The word <code>poke</code> takes a Quackery object, a nest, and an offset as arguments and returns a new nest, similar to the argument nest, except with the contents of the offset location replaced by the specified object. (Nests are immutable except under specific circumstances.)
 
Illustrating this as a dialogue in the shell (the Quackery REPL.)
 
<pre>/O> [ 10 11 12 13 14 ] is mynest ( create a named nest containing numbers )
... ' mynest 2 peek echo ( print the content of item #2 in mynest )
...
12
Stack empty.
 
/O> 99999 ' mynest 2 poke echo ( replace 12 with 99999 and print result )
...
[ 10 11 99999 13 14 ]
Stack empty.</pre>
 
<code>quid</code> returns a numerical value associated with an object in Quackery. See the discussion of <code>id()</code> in the [[Address of a variable#Python|Python entry]]. Quackery is implemented in Python, and <code>quid</code> in Quackery is equivalent to <code>id()</code> in Python.
 
=={{header|QB64}}==
Line 2,289 ⟶ 2,318:
 
You can, of course, assign a variable of one reference type to another which under the hood copies the pointer so that both variables access the same data store.
<syntaxhighlight lang="ecmascriptwren">var a = [1, 2, 3, 4]
var b = a // now 'a' and 'b' both point to the same List data
b[3] = 5
Line 2,415 ⟶ 2,444:
 
=={{header|Zig}}==
{{works with|Zig|0.11.0}}
<syntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
Line 2,422 ⟶ 2,452:
var address_of_i: *i32 = &i;
 
try stdout.print("{x}\n", .{@ptrToIntintFromPtr(address_of_i)});
}</syntaxhighlight>
 
9,476

edits