|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Pointer OperationsThe operations performed on pointers are limited to assignment and comparison. Assignment If two pointers ptr1 and ptr2 are of the same type, then the following statement is valid: ptr1:= ptr2; The effect of this statement is to redirect the pointer ptr1 to make it point to the same location pointed to by ptr2. The location which was pointed to by ptr1, before the assignment, is now inaccessible (unless it was pointed to by another pointer). When you dispose these pointers, you need to dispose only one, because they point to the same location. A second DISPOSE statement will result in an error message. Obviously, the previous assignment is totally different from the following assignment: ptr1^:= ptr2^; which means copying the contents of the location pointed to by ptr2 into the location pointed to by ptr1. The two pointers, however, may still be pointing to the same original locations. Comparison You may use the boolean operators = or <> to compare two pointers; for example, IF ptr1 = ptr2 THEN.. The boolean expression ptr1 = ptr2 is true if the two pointers are pointing to the same memory location. In the following program these features are demonstrated. { -------------------------- figure 11-3 ------------------------------ } PROGRAM PointerExample2(OUTPUT); TYPE intptr = ^INTEGER; realptr = ^REAL; VAR MyIntegerPointer, AnotherIntPointer:intptr; MyRealPointer :realptr; BEGIN NEW(MyIntegerPointer); NEW(MyRealPointer); NEW(AnotherIntPointer); MyRealPointer^:= 2.25; MyIntegerPointer^:= 500; AnotherIntPointer^:= 400; { Copy contents of locations:} MyRealPointer^:= MyIntegerPointer^; { Redirect MyIntegerPointer:} MyIntegerPointer:= AnotherIntPointer; { Display results } WRITELN('MyRealPointer is pointing to: ', MyRealPointer^:2:2); WRITELN; {Check if the two pointers point to the same location:} IF (MyIntegerPointer = AnotherIntPointer) THEN WRITELN('Yes, The two integer pointers are pointing to the same location.'); WRITELN('MyIntegerPointer is pointing to: ', MyIntegerPointer^); WRITELN('AnotherIntPointer is pointing to: ', AnotherIntPointer^); WRITELN; { Note: The DISPOSE procedure is not necessary for any pointer in this program. } DISPOSE(MyIntegerPointer); DISPOSE(MyRealPointer); {DISPOSE(AnotherIntPointer);} {illegal now..} WRITELN('Press any key to continue...'); READLN END. When you run this program, it will display the following messages: MyRealPointer is pointing to: 500.00 Yes, The two integer pointers are pointing to the same location. MyIntegerPointer is pointing to: 400 AnotherIntPointer is pointing to: 400 Press any key to continue... The following are the operations that took place in the program (also refer to the diagram below):
Pointers to RecordsYou can declare a pointer to a record in the same way you do with other types of data. It is preferred to define the pointer type in the TYPE section, TYPE emprec = RECORD ID :INTEGER; Wage : REAL; END; empptr = ^emprec; then, declare the pointer variables in the VAR section VAR ptr1, ptr2:empptr; With records, you may use all pointer operations in the same way you do with other pointer types. But there are some restrictions:
In the following program, the two record pointers, ptr1 and ptr2, are used to access the record fields; then, one of the pointers is redirected to point to the same record as the other. { --------------------------- figure 11-5 ---------------------------- } PROGRAM PointersToRecords(OUTPUT); TYPE emprec = RECORD ID :INTEGER; Wage : REAL; END; empptr = ^emprec; VAR ptr1, ptr2: empptr; BEGIN NEW(ptr1); NEW(ptr2); { Assign values to the fields } ptr1^.ID:= 123; ptr1^.Wage:= 25.5; ptr2^.ID:= 456; ptr2^.Wage:= 33.25; {Print contents:} WRITELN('Before redirection of ptr1:'); WRITELN('Ptr1 points to ID: ', ptr1^.ID, ', and Wage: $', ptr1^.Wage:2:2); WRITELN('Ptr2 points to ID: ', ptr2^.ID, ', and Wage: $', ptr2^.Wage:2:2); {Redirect ptr1:} ptr1:= ptr2; {Print contents:} WRITELN; WRITELN('After redirection of ptr1:'); WRITELN('Ptr1 points to ID: ', ptr1^.ID, ', and Wage: $', ptr1^.Wage:2:2); WRITELN('Ptr2 points to ID: ', ptr2^.ID, ', and Wage: $', ptr2^.Wage:2:2); WRITELN('Press any key to continue...'); READLN END. When you run this program, the following results are displayed on the screen: Before redirection of ptr1: Ptr1 points to ID: 123, and Wage: $25.50 Ptr2 points to ID: 456, and Wage: $33.25 After redirection of ptr1: Ptr1 points to ID: 456, and Wage: $33.25 Ptr2 points to ID: 456, and Wage: $33.25 Press any key to continue...
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |