Pointers and references: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Fortran}}: Fixed typo (existance → existence))
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 97:
print((hay stack))</lang>
Output: straw straw noodle straw straw
 
=={{header|AutoHotkey}}==
The address of the variable structure itself cannot be changed using built in methods.
Line 210 ⟶ 211:
return i;
}</lang>
 
=={{header|C++}}==
 
'''With pointers:'''
See 'C' example above.
 
C++ specific alternative to "The following code creates a pointer to an int variable":
<lang cpp>int* pointer2(&var);</lang>
 
'''With references:'''
 
The following code create a reference to an int variable:
<lang cpp>int var = 3;
int& ref = var;
// or alternatively:
int& ref2(var);</lang>
 
Access the integer variable through the reference
<lang cpp>int v = ref; // sets v to the value of var, that is, 3
ref = 42; // sets var to 42</lang>
 
References cannot be changed to refer to other objects, and cannot (legally) made to refer to no object.
 
Get a reference to the first element of an array:
<lang cpp>int array[10];
int& ref3 = array[0];</lang>
 
Changing the reference to refer to another object of the array is not possible.
 
Accessing another object of the array through the reference:
<lang cpp>v = (&ref)[3]; // read value of array[3]; however doing this is bad style</lang>
 
=={{header|c sharp|C#}}==
Line 281 ⟶ 251:
 
{{omit from|Clojure}}
 
=={{header|C++}}==
 
'''With pointers:'''
See 'C' example above.
 
C++ specific alternative to "The following code creates a pointer to an int variable":
<lang cpp>int* pointer2(&var);</lang>
 
'''With references:'''
 
The following code create a reference to an int variable:
<lang cpp>int var = 3;
int& ref = var;
// or alternatively:
int& ref2(var);</lang>
 
Access the integer variable through the reference
<lang cpp>int v = ref; // sets v to the value of var, that is, 3
ref = 42; // sets var to 42</lang>
 
References cannot be changed to refer to other objects, and cannot (legally) made to refer to no object.
 
Get a reference to the first element of an array:
<lang cpp>int array[10];
int& ref3 = array[0];</lang>
 
Changing the reference to refer to another object of the array is not possible.
 
Accessing another object of the array through the reference:
<lang cpp>v = (&ref)[3]; // read value of array[3]; however doing this is bad style</lang>
 
=={{header|COBOL}}==
Line 743 ⟶ 744:
println(xx) # Prints 7
</lang>
 
 
=={{header|Kotlin}}==
Line 825:
2
</pre>
 
=={{header|Lua}}==
Lua does not have pointers but it is worth remembering that assigning a table to a new variable creates a reference to that table, not a copy of it.
<lang Lua>local table1 = {1,2,3}
local table2 = table1
table2[3] = 4
print(unpack(table1))</lang>
{{out}}
<pre>1 2 4</pre>
 
=={{header|M2000 Interpreter}}==
Line 1,018 ⟶ 1,027:
CheckGroupRef
</lang>
 
=={{header|Lua}}==
Lua does not have pointers but it is worth remembering that assigning a table to a new variable creates a reference to that table, not a copy of it.
<lang Lua>local table1 = {1,2,3}
local table2 = table1
table2[3] = 4
print(unpack(table1))</lang>
{{out}}
<pre>1 2 4</pre>
 
=={{header|Modula-3}}==
Line 1,215:
my $arrayref = ['an', 'array'];
my $hashref = { firstkey => 'a', secondkey => 'hash' }</lang>
 
=={{header|Perl 6}}==
In Perl 6 all non-native values are boxed and accessed via implicit references. (This is like Java or Python, but unlike C or Perl 5, which use explicit referencing and dereferencing.) Variables are references to containers that can contain references to other values. Basic binding (aliasing) of references to names is supported via the <tt>:=</tt> operator, while assignment to mutable containers implies a dereference from the name to the container, followed by copying of values rather than by duplicating pointers. (Assignment of a bare object reference copies the reference as if it were a value, but the receiving container automatically dereferences as necessary, so to all appearances you are putting the object itself into the destination rather than its reference, and we just think the object can be in more than one place at the same time.)
 
<lang perl6>my $foo = 42; # place a reference to 42 in $foo's item container
$foo++; # deref $foo name, then increment the container's contents to 43
$foo.say; # deref $foo name, then $foo's container, and call a method on 43.
 
$foo := 42; # bind a direct ref to 42
$foo++; # ERROR, cannot modify immutable value
 
my @bar = 1,2,3; # deref @bar name to array container, then set its values
@bar»++; # deref @bar name to array container, then increment each value with a hyper
@bar.say; # deref @bar name to array container, then call say on that, giving 2 3 4
 
@bar := (1,2,3); # bind name directly to a List
@bar»++; # ERROR, parcels are not mutable</lang>
References to hashes and functions work more like arrays, insofar as a method call acts directly on the container, not on what the container contains. That is, they don't do the extra dereference implied by calling a method on a scalar variable.
 
To the first approximation, Perl 6 programmers do not think about references much; since everything is a reference, and value semantics are emulated by assign and other mutating operators, the ubiquitous references are largely transparent to the Perl 6 programmer most of the time.
 
=={{header|Phix}}==
Line 1,387 ⟶ 1,367:
q->p->j = s->t->k + 3; /* to say i1=i2+3 */
</lang>
 
 
=={{header|Pop11}}==
Line 1,543 ⟶ 1,522:
 
In addition, Racket has a representation of traditional C pointers as part of its FFI, but this is intended only for dealing with foreign code.
 
=={{header|Raku}}==
(formerly Perl 6)
In Perl 6 all non-native values are boxed and accessed via implicit references. (This is like Java or Python, but unlike C or Perl 5, which use explicit referencing and dereferencing.) Variables are references to containers that can contain references to other values. Basic binding (aliasing) of references to names is supported via the <tt>:=</tt> operator, while assignment to mutable containers implies a dereference from the name to the container, followed by copying of values rather than by duplicating pointers. (Assignment of a bare object reference copies the reference as if it were a value, but the receiving container automatically dereferences as necessary, so to all appearances you are putting the object itself into the destination rather than its reference, and we just think the object can be in more than one place at the same time.)
 
<lang perl6>my $foo = 42; # place a reference to 42 in $foo's item container
$foo++; # deref $foo name, then increment the container's contents to 43
$foo.say; # deref $foo name, then $foo's container, and call a method on 43.
 
$foo := 42; # bind a direct ref to 42
$foo++; # ERROR, cannot modify immutable value
 
my @bar = 1,2,3; # deref @bar name to array container, then set its values
@bar»++; # deref @bar name to array container, then increment each value with a hyper
@bar.say; # deref @bar name to array container, then call say on that, giving 2 3 4
 
@bar := (1,2,3); # bind name directly to a List
@bar»++; # ERROR, parcels are not mutable</lang>
References to hashes and functions work more like arrays, insofar as a method call acts directly on the container, not on what the container contains. That is, they don't do the extra dereference implied by calling a method on a scalar variable.
 
To the first approximation, Perl 6 programmers do not think about references much; since everything is a reference, and value semantics are emulated by assign and other mutating operators, the ubiquitous references are largely transparent to the Perl 6 programmer most of the time.
 
=={{header|REXX}}==
Line 1,570:
 
Incidentally, Scala's call by name is implemented using call by value, with the value being a (pointer to a) function object that returns the result of the expression
 
=={{header|Sidef}}==
A simple example of passing a variable-reference to a function:
Line 1,646 ⟶ 1,647:
char+ #! Adds one char
10 chars + #! Adds 10 chars
 
=={{header|VBA}}==
Formally VBA does not have pointers or references. Objects are created with the "New" keyword, and assignments require the keyword "Set". Getting the memory address of a pointer is not documented in VBA.
<lang vb>Dim samplevariable as New Object
Dim anothervariable as Object
Set anothervariable = sameplevariable</lang>
 
=={{header|XPL0}}==
Line 1,681 ⟶ 1,688:
]</lang>
 
=={{header|VBA}}==
Formally VBA does not have pointers or references. Objects are created with the "New" keyword, and assignments require the keyword "Set". Getting the memory address of a pointer is not documented in VBA.
<lang vb>Dim samplevariable as New Object
Dim anothervariable as Object
Set anothervariable = sameplevariable</lang>
=={{header|zkl}}==
Pointers don't exist and neither do conventional references but mutable containers can act like references: List, Ref (strong reference), GarbageMan.WeakRef (weak reference) and others.
10,327

edits