Pointers and references: Difference between revisions

Content deleted Content added
Tikkanz (talk | contribs)
m →‎{{header|J}}: formatting
m Fixed lang tags.
Line 8:
 
Create a pool specific access type for an integer
<lang ada>type Int_Access is access Integer;
typeInt_Acc : Int_Access is:= accessnew Integer (5);</lang>
Int_Acc : Int_Access := new Integer (5);
</lang>
A pool specific access type can be constrained to never be null. For such pointers the compiler can omit checks of being null upon dereferencing.
<lang ada>type Safe_Int_Access is not null access Integer;</lang>
<lang ada>
type Safe_Int_Access is not null access Integer;
</lang>
General access types can deal with objects allocated in any pool as well as ones on the stack. For example, create a pointer to a stack-allocated integer
<lang ada>declare
declare
type Int_Ptr is access all Integer;
Ref : Int_Ptr;
Var : aliased Integer := 3;
begin
Ref := Var'Access;</lang>
</lang>
The attribute 'Access (and also 'Unchecked_Access) is used to get a pointer to the object. Note that the object has to be declared ''aliased '' when it has to be referenced by a pointer. General access types can also be constrained to exclude null:
<lang ada>type Safe_Int_Ptr is not null access all Integer;</lang>
<lang ada>
type Safe_Int_Ptr is not null access all Integer;
</lang>
===Addresses===
Ada does not provide pointer arithmetic, but does allow evaluation of the address
<lang ada>Var : Integer;
Var_Address : Address := Var'Address;</lang>
Var : Integer;
Var_Address : Address := Var'Address;
</lang>
Addresses support operations of comparison, addition, and subtraction. Ada also supports conversion between address types and a predefined subtype of Integer named Integer_Address. This accommodates the conversion to a linear addressing of any hardware address scheme including address:offset used in the 8086 processor.
 
Ada allows the specification of a starting address for any object
<lang ada>-- Demonstrate the overlay of one object on another
<lang ada>
-- Demonstrate the overlay of one object on another
A : Integer;
B : Integer;
for B'Address use A'Address;
-- A and B start at the same address</lang>
</lang>
===References===
References in Ada are achieved through object renaming declarations. A renaming produces a new view to the object:
<lang ada>type Container is array (Positive range <>) of Element;
<lang ada>
type Container is array (Positive range <>) of Element;
for I in Container'Range loop
declare
Line 55 ⟶ 42:
... -- Here Item is a reference to Container (I)
end;
end loop;</lang>
</lang>
=={{header|ALGOL 68}}==
The following code creates a pointer to an INT variable:
<lang algol68>INT var := 3;
REF INT pointer := var;</lang>
Access the integer variable through the pointer:
<lang algol68>INT v = pointer; # sets v to the value of var (i.e. 3) #
REF INT(pointer) := 42; # sets var to 42 #</lang>
Change the pointer to refer to another object:
<lang algol68>INT othervar;
pointer := othervar;</lang>
Change the pointer to not point to any object:
<lang algol68>pointer := NIL; # 0 cannot be cast to NIL #</lang>
Get a pointer to the first element of an array:
<lang algol68>[9]INT array;
pointer := array[LWB array];</lang>
There is no pointer arithmetic, eg no p +:=3
 
Line 77 ⟶ 63:
 
The following code creates a constant reference to an INT variable, effectively an alias:
<lang algol68>REF INT alias = var;</lang>
Access the integer variable through the reference:
<lang algol68>INT v2 = alias; # sets v2 to the value of var, that is, 3 #
alias := 42; # sets var to 42 #</lang>
Constand references cannot be changed to refer to other objects.
 
Pointers can be compared, but only for basic equality:
<lang algol68>printf(($"alias "b("IS","ISNT")" var!"l$, alias IS var));</lang>
Output: alias IS var!
 
Get a reference to the first element of an array:
<lang algol68>[9]INT array2;
REF INT ref3 = array2[LWB array2];</lang>
Changing the reference to refer to another object of the array is not possible.
 
ALGOL 68 also allows pointers to slices of rows and/or columns of arrays:
<lang algol68>[9,9]INT sudoku;
REF [,]INT middle;
middle := sudoku[4:6,4:6];</lang>
 
This includes pointers to sliced character arrays:
<lang algol68>[30]CHAR hay stack := "straw straw needle straw straw";
REF[]CHAR needle = hay stack[13:18];
needle[2:3] := "oo";
print((hay stack))</lang>
Output: straw straw noodle straw straw
=={{header|AutoHotkey}}==