POINTERS
Pointers of the same type may be equated and assigned to each other.
Consider the following program
program pointers3( output ); type int_pointer = ^integer; var iptr1, iptr2 : int_pointer; begin new( iptr1 ); new( iptr2 ); iptr1^ := 10; iptr2^ := 25; writeln('the value of iptr1 is ', iptr1^); writeln('the value of iptr2 is ', iptr2^); dispose( iptr1 ); iptr1 := iptr2; iptr1^ := 3; writeln('the value of iptr1 is ', iptr1^); writeln('the value of iptr2 is ', iptr2^); dispose( iptr2 ); end.The lines
new( iptr1 ); new( iptr2 );creates two integer pointers named iptr1 and iptr2. They are not associated with any dynamic variables yet, so pictorially, it looks like,
The lines
iptr1^ := 10; iptr2^ := 25;assigns dynamic variables to each of the integer variables. Pictorially, it now looks like,
The lines
dispose( iptr1 ); iptr1 := iptr2;remove the association of iptr1 from the dynamic variable whose value was 10, and the next line makes iptr1 point to the same dynamic variable that iptr2 points to. Pictorially, it looks like,
The line
iptr1^ := 3;assigns the integer value 3 to the dynamic variable associated with iptr1. In effect, this also changes iptr2^. Pictorially, it now looks like,
the value of iptr1 is 10 the value of iptr2 is 25 the value of iptr1 is 3 the value of iptr2 is 3