Pointers and references: Difference between revisions
Content deleted Content added
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;
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>▼
▲type Safe_Int_Access is not null access Integer;
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
type Int_Ptr is access all Integer;
Ref : Int_Ptr;
Var : aliased Integer := 3;
begin
Ref := Var'Access;</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>▼
▲type Safe_Int_Ptr is not null access all Integer;
===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_Address : Address := Var'Address;
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▼
▲-- 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>
===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;▼
▲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>
=={{header|ALGOL 68}}==
The following code creates a pointer to an INT variable:
<lang
Access the integer variable through the pointer:
<lang
Change the pointer to refer to another object:
<lang
Change the pointer to not point to any object:
<lang
Get a pointer to the first element of an 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
Access the integer variable through the reference:
<lang
Constand references cannot be changed to refer to other objects.
Pointers can be compared, but only for basic equality:
<lang
Output: alias IS var!
Get a reference to the first element of an array:
<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
This includes pointers to sliced character arrays:
<lang
Output: straw straw noodle straw straw
=={{header|AutoHotkey}}==
|