Pascal - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Pascal Pointers

Pascal Pointers

shape Description

Any compiler need to store the variables in a memory.The memory address given to a particular variable is called as a Pointer. Computer’s memory consists of 8 bit bytes in a continuous sequence. Portions of this memory are reserved when a program starts to store a program code, variables, functions, arguments, local variables and so on. The storage for each variable must reside in memory, and where it resides is defined by an address. Every variable has an address even if it is not used which is just the offset from the base address to the location where the variable storage resides. Now, Pascal, C provides a special category of variables specifically for handling these addresses. So these are address variables and they are called Pointers.

shape Conceptual Figure

The simplest way to start using pointers is to get the address of some variable.This variable is stored on the memory stack, and there is no need to think about where or how its storage was provided. But its address can be obtained using the address of operator. In the above figure, a is the variable having value 10 located at an address 1000. This address is stored in another variable “ptr” called as pointer variable located at an address 2000.So, the datatype should be same for normal variable and pointer variable.

shape Syntax

The following is the syntax for the pointer in pascal. [c] type ptr-identifier = ^base-variable-type;[/c] Here the pascal type will use up arrow mark for base variable type. The pointer variable declaration will be as follows. [c]var p1, p2, ... : ptr-identifier;[/c] The following is the way to declare with type and variable. [c] type Cptr = ^char; date-ptr = ^ date; End; var a, b : Rptr; d: date-ptr; [/c]

shape Example

The following is an example for the pointer in pascal. [c]program Pointers; var number: integer; iptr: ^integer; begin number := 245; writeln('Number is: ', number); iptr := @number; writeln('iptr points to a value: ', iptr^); iptr^ := 300; writeln('Number is: ', number); writeln('iptr points to a value: ', iptr^); end.[/c] In the above example type and variables are declared, where iptr will store the value of the variable number and it will be returned, @ is the address operator. Now compile the code result will be as follows. [c]Number is: 245 iptr points to a value: 245 Number is: 300 iptr points to a value: 300[/c] The following is an example to declare NIL pointers, some times it's better to use nil pointer in case do not know the exact size as follows. [c]var number: integer; iptr: ^integer; x: ^word; begin iptr := nil; x := addr(iptr); writeln('the vaule of iptr is ', x^); end.[/c] Output: Now the result will be zero. [c]the vaule of iptr is 0[/c]

Summary

shape Key Points

  • The @ is a address operator in pascal language.
  • The Nil is the keyword to get the null pointer value.
  • Simply pointer is used to store the address of a variable.