Address of a variable: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Odin variant)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(17 intermediate revisions by 9 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 X(8) VALUE '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}}==
{{works with|BASIC|7.0 (C-128)}}
The address of a variable in BASIC is readonly, but accessible via the <tt>POINTER</tt> function.
 
<syntaxhighlight lang="basic">100 REM ALLOCATE ALL VARS FIRST SO THEY DON'T MOVE
110 X=123:Y=456:PX=0:PY=0:I=0:T=0
120 PRINT "BEFORE:X="XCHR$(20)",Y="Y
130 PX=POINTER(X):PY=POINTER(Y)
140 REM VARS ARE STORED IN RAM BANK 1
150 BANK 1
160 FOR I=0 TO 6
170 : T = PEEK(PY+I)
180 : POKE PY+I,PEEK(PX+I)
190 : POKE PX+I,T
200 NEXT I
210 PRINT "AFTER: X="XCHR$(20)",Y="Y</syntaxhighlight>
 
{{works with|BASIC|2.0 (VIC-20, C-64)}}
With older machines, there's no built-in mechanism in BASIC to find a variable's address, but you can use the internal state of the BASIC interpreter to achieve similar results. Here's a VIC-20/C-64 version that works the same as the above C-128 program:
 
<syntaxhighlight lang="basic">100 X=PEEK(71)+256*PEEK(72):PX=X:X=123
110 Y=PEEK(71)+256*PEEK(72):PY=Y:Y=456
120 PRINT "BEFORE:X ="XCHR$(20)", Y ="Y
130 FOR I=0 TO 6
140 : T = PEEK(PY+I)
150 : POKE PY+I,PEEK(PX+I)
160 : POKE PX+I,T
170 NEXT I
180 PRINT "AFTER: X ="XCHR$(20)", Y ="Y</syntaxhighlight>
 
They same idea should work on other Commodore computers as well, though at least the address being <tt>PEEK</tt>ed will have to change.
{{Out}}
 
<pre>BEFORE:X= 123,Y= 456
AFTER: X= 456,Y= 123</pre>
 
=={{header|Common Lisp}}==
Line 920 ⟶ 962:
Value of i = 575
</pre>
 
=={{header|GDScript}}==
{{works with|Godot|4.0}}
 
GDScript does not expose addresses, however there are reference identifiers (RIDs).
 
<syntaxhighlight lang="gdscript">
extends MainLoop
 
 
func _process(_delta: float) -> bool:
var a := Node.new()
var b := Node.new()
 
# a and b are different objects with different RIDs
print(a.get_instance_id())
print(b.get_instance_id())
 
assert(a != b)
assert(a.get_instance_id() != b.get_instance_id())
 
# Set b to refer to the same object as a
b.free()
b = a
 
# a and b are now the same object and have the same RID
print(a.get_instance_id())
print(b.get_instance_id())
 
assert(a == b)
assert(a.get_instance_id() == b.get_instance_id())
 
a.free()
return true # Exit
 
</syntaxhighlight>
 
=={{header|Go}}==
Line 1,167 ⟶ 1,245:
2 2
sin(x) + cos(x)</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
From version 12, VarPtr() can return address for variables and array items.
 
We can't set an address to a variable. We can link a variable name with an existing variable. Is the same as passing by reference.
 
<syntaxhighlight lang="m2000 interpreter">
module checkVarptr {
declare GetMem8 lib "msvbvm60.GetMem8" {Long addr, Long retValue}
long long x=1234567812345678&&, z
dim k(2,2,2) as long long
call GetMem8(VarPtr(x), VarPtr(z))
call GetMem8(VarPtr(x), VarPtr(k(1,0,1)))
print z=x
print k(1,0,1)=x
link z to m
print VarPtr(z)=VarPtr(m)
checkref(&z)
sub checkref(&p)
print VarPtr(z)=VarPtr(p)
end sub
}
checkVarptr
module checkVarptr {
// Using byref at GemMem8 signature
declare GetMem8 lib "msvbvm60.GetMem8" {long &addr, long &retValue}
declare PutMem8 lib "msvbvm60.PutMem8" {long &addr, retValue as long long}
long long x=1234567812345678&&, z
dim k(2,2,2) as long long
call GetMem8(&x, &z)
call GetMem8(&x, &k(1,0,1))
print z=x
print k(1,0,1)=x
checkref(&z)
print z=987654321987654321&&
sub checkref(&p)
print VarPtr(z)=VarPtr(p)
call PutMem8(&p, 987654321987654321&&)
end sub
}
checkVarptr
</syntaxhighlight>
{{out}}
<pre>
True
True
True
True
</pre>
 
=={{header|Modula-2}}==
Line 1,235 ⟶ 1,362:
=={{header|Oberon-2}}==
===Get Address===
<syntaxhighlight lang="oberon2">
<pre>
VAR a: LONGINT;
VAR b: INTEGER;
Line 1,241 ⟶ 1,368:
b := 10;
a := SYSTEM.ADR(b); (* Sets variable a to the address of variable b *)
</syntaxhighlight>
</pre>
 
===Set Address===
<syntaxhighlight lang="oberon2">
<pre>
SYSTEM.PUT(a, b); (* Sets the address of b to the address of a *)
</syntaxhighlight>
</pre>
 
=={{header|OCaml}}==
Line 1,608 ⟶ 1,735:
VALUE IS NOW 0BA3 AND BAR IS NOW 0BA3
POINTER IS 034D AND VALUE IS CAFE</pre>
 
=={{header|Plain English}}==
To get the address of a variable, use:
<syntaxhighlight lang="text">[a variable]'s whereabouts</syntaxhighlight>
Note that this address cannot be modified.
 
=={{header|PowerBASIC}}==
Line 1,654 ⟶ 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,135 ⟶ 2,290:
 
Dim ptrX As New IntPtr(&HA100)
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
fn main() {
var := 42
address_of_var := &var // pointer to variable
println(&address_of_var) // reference
println(*address_of_var) // dereference a reference // 42
}
</syntaxhighlight>
 
=={{header|Wart}}==
Line 2,153 ⟶ 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,279 ⟶ 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,286 ⟶ 2,452:
var address_of_i: *i32 = &i;
 
try stdout.print("{x}\n", .{@ptrToIntintFromPtr(address_of_i)});
}</syntaxhighlight>
 
9,476

edits