menu prev next

POINTERS
Pointers which do not reference any memory location should be assigned the value nil. Consider the following program, which expands on the previous program.


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

	var	iptr : int_pointer;
	begin
		new( iptr );
		iptr^ := 10;
		writeln('the value is ', iptr^);
		dispose( iptr );
		iptr := nil;
		if iptr = nil
		   then writeln('iptr does not reference any variable')
		else
		   writeln('The value of the reference for iptr is ', iptr^)
	end.

The line
	iptr := nil;
assigns the value nil to the pointer variable iptr. This means that the pointer is valid and stil exists, but it does not point to any memory location or dynamic variable.

The line

	if iptr = nil
tests iptr to see if its a nil pointer, ie, that it is not pointing to a valid reference. This test is very useful and will come in use later on when we want to construct more complex data types like linked lists.


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

1