Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Pointer Operations

The 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):

1.  The variable “MyRealPointer^” is assigned the value 2.25.
2.  The variable “MyIntegerPointer^” is assigned the value 500.
3.  The variable “AnotherIntPointer^” is assigned the value 400.
4.  The contents of “MyIntegerPointer^” are copied into “MyRealPointer^;” therefore, its stored value becomes 500.00.
5.  The pointer “MyIntegerPointer” is redirected to point to the same location pointed to by “AnotherIntPointer,” which contains the value 400.


NOTE:  You cannot read a pointer value. You can only read the contents pointed to by the pointer.

Pointers to Records

You 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:

1.  When you create a pointer to a record, the pointer is bound to this specific record type and may not be used with another record type.
2.  The relational expression
         ptr1^ = ptr2^

is invalid, as records cannot be compared using relational expressions. You may, however, compare the two pointers to check if they are pointing to the same record.
         ptr1 = ptr2,or
         ptr1 <> ptr2
3.  The field contents are accessed using fielded variables
         ptr1^.ID
         ptr1^.Wage
         ptr2^.ID
         ptr2^.Wage

or using a WITH statement
         WITH ptr1^ DO
                ID := 123;
                Wage := 22.5;
                ...

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...


Previous Table of Contents Next


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.