Address of a variable: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Commodore BASIC}}: Add example for C64)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(10 intermediate revisions by 6 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}}==
Line 567 ⟶ 573:
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. You have to work around the fact that the interpreter is actively interpreting the running program: for example, you canHere't store the result of the lookup directly intos a variable,VIC-20/C-64 because the assignment statement will have changed the pointer toversion that ofworks the variablesame being assigned to byas the timeabove BASICC-128 executes the <tt>PEEK</tt>.program:
 
Here's a C-64 version that works the same as the above C-128 program:
 
<syntaxhighlight lang="basic">100 X=123:Y=456
110 SYS 45195 X:POKE 249,PEEK(71):POKE250,PEEK(72)
120 PX=PEEK(249)+256*PEEK(250)
130 SYS 45195 Y:POKE 249,PEEK(71):POKE250,PEEK(72)
140 PY=PEEK(249)+256*PEEK(250)
150 PRINT "BEFORE:X ="XCHR$(20)", Y ="Y
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>
 
<syntaxhighlight lang="basic">100 X=PEEK(71)+256*PEEK(72):PX=X:X=123
It also works on the VIC-20 if you change the <tt>SYS</tt> address to 53387, and with a few other adjustments the same idea will work on other Commodore machines.
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}}
 
Line 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,313 ⟶ 1,362:
=={{header|Oberon-2}}==
===Get Address===
<syntaxhighlight lang="oberon2">
<pre>
VAR a: LONGINT;
VAR b: INTEGER;
Line 1,319 ⟶ 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,737 ⟶ 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,246 ⟶ 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,372 ⟶ 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,379 ⟶ 2,452:
var address_of_i: *i32 = &i;
 
try stdout.print("{x}\n", .{@ptrToIntintFromPtr(address_of_i)});
}</syntaxhighlight>
 
9,476

edits