Pointers and references: Difference between revisions
Content added Content deleted
m (→{{header|J}}: formatting) |
m (Fixed lang tags.) |
||
Line 8: | Line 8: | ||
Create a pool specific access type for an integer |
Create a pool specific access type for an integer |
||
<lang ada> |
<lang ada>type Int_Access is access Integer; |
||
Int_Acc : Int_Access := new 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. |
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> |
|||
⚫ | |||
</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 |
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> |
<lang ada>declare |
||
declare |
|||
type Int_Ptr is access all Integer; |
type Int_Ptr is access all Integer; |
||
Ref : Int_Ptr; |
Ref : Int_Ptr; |
||
Var : aliased Integer := 3; |
Var : aliased Integer := 3; |
||
begin |
begin |
||
Ref := Var'Access; |
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: |
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> |
|||
⚫ | |||
</lang> |
|||
===Addresses=== |
===Addresses=== |
||
Ada does not provide pointer arithmetic, but does allow evaluation of the address |
Ada does not provide pointer arithmetic, but does allow evaluation of the address |
||
<lang ada> |
<lang ada>Var : Integer; |
||
⚫ | |||
Var : Integer; |
|||
⚫ | |||
</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. |
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 |
Ada allows the specification of a starting address for any object |
||
⚫ | |||
<lang ada> |
|||
⚫ | |||
A : Integer; |
A : Integer; |
||
B : Integer; |
B : Integer; |
||
for B'Address use A'Address; |
for B'Address use A'Address; |
||
-- A and B start at the same address |
-- A and B start at the same address</lang> |
||
</lang> |
|||
===References=== |
===References=== |
||
References in Ada are achieved through object renaming declarations. A renaming produces a new view to the object: |
References in Ada are achieved through object renaming declarations. A renaming produces a new view to the object: |
||
⚫ | |||
<lang ada> |
|||
⚫ | |||
for I in Container'Range loop |
for I in Container'Range loop |
||
declare |
declare |
||
Line 55: | Line 42: | ||
... -- Here Item is a reference to Container (I) |
... -- Here Item is a reference to Container (I) |
||
end; |
end; |
||
end loop; |
end loop;</lang> |
||
</lang> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
The following code creates a pointer to an INT variable: |
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: |
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: |
Change the pointer to refer to another object: |
||
<lang algol68>INT othervar; |
|||
pointer := othervar;</lang> |
|||
Change the pointer to not point to any object: |
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: |
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 |
There is no pointer arithmetic, eg no p +:=3 |
||
Line 77: | Line 63: | ||
The following code creates a constant reference to an INT variable, effectively an alias: |
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: |
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. |
Constand references cannot be changed to refer to other objects. |
||
Pointers can be compared, but only for basic equality: |
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! |
Output: alias IS var! |
||
Get a reference to the first element of an array: |
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. |
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: |
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: |
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 |
Output: straw straw noodle straw straw |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |