Pointers and references

From Rosetta Code
Revision as of 03:40, 21 February 2007 by Nick (talk | contribs) (→‎[[Perl]]: syntax highlight)
Task
Pointers and references
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to desmonstrate common operations on pointers and references.

Ada

Ada does not have pointers, only access types.

Create a pool specific access type for an integer

 type Int_Access is access Integer;
 Int_Acc : Int_Access := new Integer(5);

Create a general access type for an integer

 type Int_Ptr is access all Integer;
 Ref : Int_Ptr;
 Var : aliased Integer := 3;
 Ref := Var'access;

Ada does not provide pointer arithmetic, but does allow evaluation of the address

 Var : Integer;
 Var_Address : Address := Var'address;

Addresses support operations of comparison, addition, and subtraction Ada also supports conversion between address types and a pre-defined subtype of Integer named Integer_Address. This accomodates 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

 -- 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

C++

With pointers:

The following code creates a pointer to an int variable

 int var = 3;
 int* pointer = &var;
 // or alternatively:
 int* pointer2(&var);

Access the integer variable through the pointer:

 int v = *pointer; // sets v to the value of var (i.e. 3)
 *pointer = 42; // sets var to 42

Change the pointer to refer to another object

 int othervar;
 pointer = &othervar;

Change the pointer to not point to any object

 pointer = 0;
 // or alternatively:
 pointer = NULL;

Get a pointer to the first element of an array:

 int array[10];
 pointer = array;
 // or alternatively:
 pointer = &array[0];

Move the pointer to another object in the array

 pointer += 3; // pointer now points to array[3]
 pointer -= 2; // pointer now points to array[1]

Access another object in the same array through the pointer

 v = pointer[3]; // accesses third-next object, i.e. array[4]
 v = pointer[-1]; // accesses previous object, i.e. array[0]
 // or alternatively
 v = *(pointer + 3); // array[4]
 v = *(pointer - 1); // array[0]

With references:

The following code create a reference to an int variable:

 int var = 3;
 int& ref = var;
 // or alternatively:
 int& ref2(var);

Access the integer variable through the reference

 int v = ref; // sets v to the value of var, that is, 3
 ref = 42; // sets var to 42

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:

 int array[10];
 int& ref3 = array[0];

Changing the reference to refer to another object of the array is not possible.

Accessing another object of the array through the reference:

 v = (&ref)[3]; // read value of array[3]; however doing this is bad style

Perl

Interpeter: Perl v5.x

References are essentially how Perl handles pointers. Any scalar value, element in an array, key of a hash, or value of a hash may contain a reference to any other variable structurer.

<highlightSyntax language=perl>

  1. start with some var definitions

my $scalar = 'a string'; my @array = ('an', 'array'); my %hash = ( firstkey => 'a', secondkey => 'hash' );

  1. make pointers

my $scalarref = \$scalar; my $arrayref = \@array; my $hashref = \%hash; </highlightSyntax>

Using a reference

<highlightSyntax language=perl>

  1. printing the value

print ${$scalar}; print $arrayref->[1]; # this would print "array" print $hashref->{'secondkey'}; # this would print "hash"

  1. changing the value

${$scalar} = 'a new string'; # would change $scalar as well $arrayref->[0] = 'an altered'; # would change the first value of @array as well $hashref->{'firstkey'} = 'a good'; # would change the value of the firstkey name value pair in %hash </highlightSyntax>

You may also create pointers or references without pointing to a previous variable.

<highlightSyntax language=perl> my $scalarref; ${$scalarref} = 'a scalar'; my $arrayref = ['an', 'array']; my $hashref = { firstkey => 'a', secondkey => 'hash' } </highlightSyntax>

Tcl

Tcl does not have pointers, however if required a similar level of indirection can be had by storing a variable name in another variable, eg.

 set var 3
 set pointer var; # assign name "var" not value 3
 set pointer;     # returns "var"
 set $pointer;    # returns 3
 set $pointer 42; # variable var now has value 42

In practice it's safer and more convenient to use array keys, eg.

 set arr(var) 3
 set pointer var
 set arr($pointer);    # returns 3
 set arr($pointer) 42; # arr(var) now has value 42