09.10.2014 Views

download pascal tutorial (pdf - Tutorials Point

download pascal tutorial (pdf - Tutorials Point

download pascal tutorial (pdf - Tutorials Point

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Following are some valid pointer declarations:<br />

type<br />

Rptr = ^real;<br />

Cptr = ^char;<br />

Bptr = ^ Boolean;<br />

Aptr = ^array[1..5] of real;<br />

date-ptr = ^ date;<br />

Date = record<br />

Day: 1..31;<br />

Month: 1..12;<br />

Year: 1900..3000;<br />

End;<br />

var<br />

a, b : Rptr;<br />

d: date-ptr;<br />

The pointer variables are dereferenced by using the same caret symbol (^). For example,<br />

the associated variable referred by a pointer rptr, is rptr^. It can be accessed as:<br />

rptr^ := 234.56;<br />

The following example will illustrate this concept:<br />

program ex<strong>Point</strong>ers;<br />

var<br />

number: integer;<br />

iptr: ^integer;<br />

begin<br />

number := 100;<br />

writeln('Number is: ', number);<br />

iptr := @number;<br />

writeln('iptr points to a value: ', iptr^);<br />

iptr^ := 200;<br />

writeln('Number is: ', number);<br />

writeln('iptr points to a value: ', iptr^);<br />

end.<br />

When the above code is compiled and executed, it produces the following result:<br />

Number is: 100<br />

iptr points to a value: 100<br />

Number is: 200<br />

iptr points to a value: 200<br />

Printing a Memory Address in Pascal<br />

In Pascal, we can assign the address of a variable to a pointer variable using the address<br />

operator (@). We use this pointer to manipulate and access the data item. However, if for<br />

TUTORIALS POINT<br />

Simply Easy Learning Page 91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!