Pascal. Pointer types Pointer and ^. Text type

Pointer types Pointer and ^. Text type


Search other resources:

1. Pointer types. Overview

In Pascal, pointer types are types that are designed to get the address of a program element. Pointer types allow you to access variables and constants in your program. The values of variables and constants of pointer types are the memory address.

The following pointer types are distinguished:

  • Pointer is a type that defines an untyped pointer. This allows you to declare a pointer to a variable or constant of any other type;
  • PANsiChar, PWideChar, PChar – designed to work with strings of the AnsiChar, WideChar, Char types, respectively;
  • type in the format ^Type, where Type is one of the standard types or user types.


2. Features of using pointer types. Declaring a pointer of type Pointer and ^

In addition to the predefined type identifiers (Pointer), it is allowed to declare a pointer type using the ^ character in the following general form:


here Type is the name of a standard type (Integer, Real, …) or a user-defined type.

To get the address of a variable, use a construction like


here variable is a variable of some type.

Accordingly, the operation of assignment to a pointer named p of the address of variable in the general case looks like this

p := @variable;

Once p has been assigned the address of variable, you can access the memory location where variable is stored. This makes it possible to change the value of variable using the pointer p in the following general form

p^ := value;

here value is the value that is assigned to variable using the p pointer.


3. Pointer to a blank address. The nil keyword. Example

As soon as a pointer is declared in the program, some arbitrary value is stored in it, which means an arbitrary address. In this case, the pointer is said to point to an undefined address. To normalize the value of a pointer not yet in use, it is assigned the value of an empty address, which is defined using the nil keyword.

The assignment operation looks like this:

ptr := nil;


  • ptr – pointer of Pointer type or ^.


  p1 : Pointer;
  p2 : ^Double;


  p1 := nil; // assigning a pointer to an empty address
  p2 := nil;



4. An example of declaring pointers to various types and using the pointer types Pointer and ^ to access variables

The example demonstrates some of the possibilities of using the pointer types Pointer and ^.

program Project1;



  // Pointer types: Pointer, ^Type
  i : Integer;
  r : Real;
  c : Char;
  p : Pointer; // A typeless pointer to a variable
  pI : ^Integer; // Pointer to Integer type
  pR : ^Real; // Pointer to Real type
  pC : ^Char; // Pointer to Char type

  // 1. Pointer to Integer type
  i := 8;
  p := @i; // Assign pointer p to the address of variable i
  pI := p; // Set pointer pI to the value of pointer p
  pI^ := 10; // Change the value of the variable i by the pointer pI
  Writeln('i = ', i); // i = 10

  // 2. Value nil
  p := nil; // the nil value is an empty address
  pI := nil;
  pC := nil;

  // 3. Using the pointer to a Real type
  pR := @r; // get the address of the variable r
  pR^ := 20.55; // Assign value by pointer to variable r
  Writeln('r = ', r:2:2); // Print the value of the variable r

  // 4. Using a pointer to a Char type
  pC := @c; // get the address of the variable c, the pC pointer points to c
  c := '+'; // assign a value to the variable c
  Writeln('c = ', pC^); // output the value of c by pointer pC


Program result

i = 10
r = 20.55
c = +


5. Text type. Working with text files. Example

The standard type Text is used to describe text files. In general, a variable declaration of type Text looks like this:

  variableName : Text;

here variableName is the name of a variable of type Text.


Our example demonstrates the use of variables of type Text. First, the string ‘Hello, world!’ Is written to the specified file myfile.txt. This string is then read and displayed.

program Project1;



  // Тип Text
  f1, f2:Text; // declare variables of type Text

  s:String; // additional variable

  // The Text type is used to describe text files
  // 1. Writing the string 'Hello, world!' to the text file 'myfile.txt'
  // 1.1. Bind variable f1 named 'myfile.txt'
  AssignFile(f1, 'myfile.txt');

  // 1.2. Open file for writing

  // 1.3. Write the string 'Hello, world!' to file f1
  Writeln(f1, 'Hello, world!');

  // 1.4. Close the file associated with the f1 variable

  // 2. Read all lines from file 'myfile.txt'
  // 2.1. Associate variable f2 with file 'myfile.txt'
  AssignFile(f2, 'myfile.txt');

  // 2.2. Open file for writing

  // 2.3. Read all lines from a file in a loop and print them to the screen
  Writeln('File ''myfile.txt'':');

  while not eof(f2) do
    Readln(f2, s);

  // 2.4. Close the file that was associated with the f2 variable


Program result

File 'myfile.txt':
Hello, world!


Related topics