Pointers in Ada
On this page
Language: Ada
Ada supports pointers, allowing you to pass references to values and records within your program.
Example
We’ll show how pointers work in contrast to values with two procedures: zeroval
and zeroptr
. zeroval
has an Integer
parameter, so arguments will be passed to it by value. zeroval
will get a copy of ival
distinct from the one in the calling procedure.
zeroptr
in contrast has an access parameter, meaning that it takes a pointer to an Integer
. The Ptr.all
code in the procedure body then dereferences the pointer from its memory address to the current value at that address. Assigning a value to a dereferenced pointer changes the value at the referenced address.
Main procedure demonstrating the usage:
To compile and run the Ada program, use the following commands:
After running the program, you should see the following output:
The zeroval
procedure doesn’t change the i
in main
, but zeroptr
does because it has a reference to the memory address for that variable.
Now that we can work with pointers in Ada, let’s learn more about the language.