[Pascal – TUT] – Posts 11 : Pointers in Pascal

Do sequencing post should have little confused Posts 10: Files in pascal - File Type was written earlier, you see here nhé.

In the course of work we often have to work with a long list, and of course we will consider using Plate immediately. However, the use of the array is not always, with the long list to a few thousand elements, such a list of employees in a large company because not enough memory. On the other hand when we declare the array, must declare the number of elements, whereas we can not know before, and of course we will have to declare the largest element is able to work enough to lead to wasted memory or a bad case of insufficient number of elements that are considered such as suspension =)).

Static variables is variable in size, data type and the address of the variable is constant, these variables exist during run. And prior to this we still use static variables, That is why wasting memory.Movements variable can change the size and memory address is allocated during run. This is our solution. hehe. But the problem is that volatility is not a fixed address can not be accessed until they are.
Pointer is transformed to contain the address of the volatility makes it accessible to fluctuations. Wow, Solution is here =)). The data type of the pointer variable called pointer type.

Now we will go into some parts of the pointer.

1. Defines the type of cursor
Syntax:

Type
	<Kieu con tro> = <^Kieu du lieu>

Example:

Type
	int = ^integer;

Noted: When defining the type of cursor pointer type structure itself (Self-point type records) we must define the type of cursor first and then define the type of record.
Example:

Type
        ControSV = ^sinhvien;
        sinhvien = record
                MSV : string;
                Hoten : string;
                Diem : real;
                next : ControSV;
        end;

2. Declare cursor variables
We can declare direct or indirect. For example, the type and ControSV sinhvien we can declare

var
        sv1 : ControSV;         {khai bao gian tiep}
        sv2 : ^sinhvien;        {khai bao truc tiep}

In addition, we can declare as follows when not care about the type of data.

var
        p : pointer;

After declaring pointer variable p is a pointer not in total exceed a specific data type.

3. Allocate memory for the variable.
We note that the pointer variable is static variables, it is computer memory allocator 4 byte. For example, to turn on the SV1 SV1 as is 1 pointer, it is allocated memory area 4 ^ SV1 new byte variable is also fluctuate and are not allocated. Ie we can understand the SV1 pointer contains the address of volatility SV1 ^.
To allocate memory for SV1 volatility we use the new operator(sv1);
pointers in pascal
NIL pointer cursor is particularly, does not point to nowhere. The value of the pointer by NILL 0 or the corresponding empty every type of cursor.

4. Operations.
the. Assignments
Two pointer can be assigned to each of the assignment ( := ) if they are the same type. In the above example we can assign:

sv1 := sv2;

In addition we can also assign

p := sv1;

but can not assign the opposite because the cursor pointer p is the general type.
We can assign any value NIL pointer public.

b. Comparisons
We only use the analogy with ( = ) and other comparisons ( <> ) the pointer type. If p and q cursor is pointing to an address of a movement (pointing to a variation) they are considered equal, the opposite would be different.

5. Point the cursor to 1 variable
When we have 1 a variable of type integer, I will use the @ operator to point the pointer p to the memory of a variable.

p := @a;

6. Retrieving data
Variable pointer contains the address of volatility, we can not enter direct pointer variable that must be made for fluctuations enter it points to.
For example, after performing access, calculated on the cursor.

uses crt;
var
        p : ^integer;
        a, s : integer;
BEGIN
        clrscr;
        write('Nhap vao mot so (kieu con tro) p^ = ');
        new(p);
        readln(p^);
        write('Nhap vao mot so (kieu so nguyen) a =  ');
        readln(a);
        s := 100*p^ + a;
        writeln('100p^ + a = : ', s);
        readln;
END.

pointers in pascal

The largest use of pointers like the preamble stated, create a linked list instead of the array. On the list of links you can see here, very full, detailed in C and. (It is like Pascal alone hehee).
And here is an article illustrated by Pascal on entry and linked lists.

Program DSLK;
uses crt;
type
        {dinh nghia kieu sinhvien}
        sinhvien = record
                MSV, Hoten : string;
                Diem : real;
        end;
        {dinh nghia Plist va list}
        Plist = ^list;
        list = record
                data : sinhvien;
                next : Plist;
        end;

var
        first, last, newp : Plist;
        ch : char;
        i : integer;
BEGIN
        clrscr;
        writeln('Danh sach sinh vien su dung con tro - danh sach lien ket');
        writeln('-------------');
        writeln;
        i := 0;
        repeat
                inc(i);
                new(newp); {cap phat o nho}
                with newp^.data do
                begin
                        write('Nhap ma sinh vien thu ', i, ' : ');
                        readln(MSV);
                        write('Nhap ho ten : ');
                        readln(Hoten);
                        write('Nhap diem tong ket : ');
                        readln(Diem);
                        write('Tiep tuc ? (y/ n)');
                        readln(ch);
                        ch := upcase(ch);
                end;

                if first = nil then     {Gan phan tu dau tien}
                        first := newp
                else                    {Gan phan tu cuoi cung}
                        last^.next := newp;
                last := newp;           {Cap nhat con tro last}
                last^.next := nil;
        until ch = 'N';
        clrscr;

        writeln('Danh sach sinh vien su dung con tro - danh sach lien ket');
        writeln('-------------');
        writeln;

        newp := first;
        writeln('STT':5, ' | ', 'Ma sinh vien':20,' | ', 'Ho va ten' : 20,' | ', 'Diem':5);
        for i:= 1 to 50 do write('-');
        writeln;
        i := 0;
        while newp <> nil do
                with newp^ do
                begin
                        inc(i);
                        writeln(i:5,' | ', data.MSV:20, ' | ', data.Hoten:20, ' | ', data.Diem:5:2);
                        newp := next;
                end;
        readln;
END.

Enter data:
linked list

Result:
linked list