Previous topic | Next topic | Ada Home Page | Index

Access variables

Access types

can declare variables of access types


    type int_pointer     is access INTEGER;
    type contact_pointer is access contact;
    PI : int_pointer;
    PC : contact_pointer;

Access Variables

use access variables to save pointers to allocated objects

Access variables are pointers

Access variables provide indirect access to data

Access variables are initialised by default to null


Accessing an object

to access the entire object pointed to by P

(If the object is a record) to access a field:

(If the object is an array) to access an element:

CONSTRAINT_ERROR if P has value null

P is not affected when the object is accessed


Assignment with access variables

can assign access variables if they access the same type

    type contact is record
        initials  : string (1..3);
        extension : integer;
    end record;

    type contact_pointer is access contact;

    P1, P2, P3, P4 : contact_pointer;

Trace the effect of these statements:

    P1 := new contact'("CJL", 8817);
    P1.extension := 8060;
    P2 := new contact;
    P3 := P1;
    P2.all := P1.all;
    P3 := null;
    P4.extension := 8060;   -- constraint error
    P4 := new INTEGER;      -- syntax error

An example compares how the same problem can be solved with and without access types. Thus you can see how the access types are used.


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96