menu prev next

POINTERS
Pointers enable us to effectively represent complex data structures, to change values as arguments to functions, to work with memory which has been dynamically allocated, and to store data in complex ways.

A pointer provides an indirect means of accessing the value of a particular data item. Lets see how pointers actually work with a simple example,


	program pointers1( output );
	type	int_pointer = ^integer;

	var	iptr : int_pointer;
	begin
		new( iptr );
		iptr^ := 10;
		writeln('the value is ', iptr^);
		dispose( iptr )
	end.

The line
	type	int_pointer = ^integer;
declares a new type of variable called int_pointer, which is a pointer (denoted by ^) to an integer.

The line

	var	iptr : int_pointer;
declares a working variable called iptr of type int_pointer. The variable iptr will not contain numeric values, but will contain the address in memory of a dynamically created variable (by using new). Currently, there is no storage space allocated with iptr, which means you cannot use it till you associate some storage space to it. Pictorially, it looks like,

The line

		new( iptr );
creates a new dynamic variable (ie, its created when the program actually runs on the computer). The pointer variable iptr points to the location/address in memory of the storage space used to hold an integer value. Pictorially, it looks like,

The line

		iptr^ := 10;
means go to the storage space allocated/associated with iptr, and write in that storage space the integer value 10. Pictorially, it looks like,

The line

		dispose( iptr )
means deallocate (free up) the storage space allocated/associated with iptr, and return it to the computer system. This means that iptr cannot be used again unless it is associated with another new() statement first. Pictorially, it looks like,


Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.
menu prev next

1