Prog_2 course- 2014 
2 bytes team 
Kinan keshkeh 
IT Engineering-Damascus University 
3rd year
POINTERS
Static Variables && Dynamic Variables 
Static Variables : 
var x:integer; 
The memory for this variables x still until program End , and doesn’t free even we don’t use it 
Ex: 
X 
Memory 
We determine their places at memory first of all 
value
Dynamic Variables : 
Memory 
The Heap 
X 
var x: ^integer; 
Ex: 
the address 
the address 
value 
The memory for this variables x in the Heap, we can take it or dispose it, so it can be free!! 
We determine their places at memory during the program
Why Dynamic?? 
1) better usage to memory 
2) to create advance data structure like trees , lists….atc
What is the pointer? 
The Definition 
The pointer is an address to a memory place 
Var ptr: ^Type; 
Lets Have an explication!! but…..
P1 
X 
Value.. 
P1^ 
P1 
Var p1,p2: ^Type; x :Type; 
P1^ 
P1 
P1^ 
X:=value 
P2 
xx 
P1^ 
P2 
New(p1); 
P1^:=x 
Value.. 
P2:=P1 
Value.. 
P1:=nil; 
Value.. 
+1
xx 
P1^ 
P2 
Dispose(p2); 
Value.. 
xx 
P1^ 
xx 
P2:=nil; 
Value.. 
+1
Program pointer on Soso; 
There is wrong ! 
Type 
pEmp=^Emp; 
Emp=Record 
eno:integer; ename:string[20]; 
esal:real; 
end; 
Var 
E:Emp; E1:pEmp; 
n,m,y:^integer; 
EXAMPLE:
Begin i:=5; new(y); y^:=50; write(y); write(^y); write(y^); y:=i y^:=i ; y^:=20; i:=y^; y:=0; dispose(y); y:=nil; new(m); new(y); m^:=30; y^:=80; n:=y; write(y^); write(n^); E1:=nil; new(E1); E1^:=E ; Read(E1^.Eno); Dispose(E1); End. 
false 
false 
TRUE 
NOTE: Without “ New(n)” !! 
80 80 
false 
TRUE
STACK {FILO} 
First In Last Out 
pringles
The Data 
next 
Nil 
TOP 
Stack=Record 
the Data: Type; 
next: Pstack; 
end; 
The Definition 
Pstack=^stack; 
Var 
Top: Pstack; 
D:Type; 
……….
Stack procedures : 
Push(s,ch) 
Pop(s,ch) 
S_Top(s,ch) 
Is_Empty(s) 
3) 
2) 
1) 
4)
EX: You have a string “KiKi” How to put/take it in /from the STACK ?! 
i 
next 
TOP 
K 
i 
K 
Nil
Stack=Record ch: char; next: Pstack; end; 
Type Pstack=^stack; 
Var Top: Pstack; st: string[4]; i:integer; x:char; 
BEGIN 
Readln(st); top:=nil; 
For i:=1 to (length(st) ) do 
push( top, st[i] ) ; 
While(top<>Nil) do 
begin 
Pop( top, x) ; write(x,’ ’); 
end; 
END. 
Program fifi;
PUSH 
Procedure push (var Top:Pstack; c:char); Var temp:Pstack; Begin end; 
new(temp); 
temp^.ch:=c; 
temp^.next:=top; 
top:=temp; 
i 
next 
TOP 
K 
i 
K 
Nil 
temp 
K 
TOP 
i 
TOP 
K 
TOP 
i
Stack=Record 
ch: char; 
next: Pstack; 
end; 
Type Pstack=^stack; 
Var Top: Pstack; st: string[4]; i:integer; x:char; 
BEGIN 
Readln(st); top:=nil; 
For i:=1 to (length(st) ) 
push( top, st[i] ) ; 
While(top<>Nil) do 
begin 
Pop( top, x) ; write(x,’ ’); 
end; 
END. 
Program fifi;
POP 
Procedure pop (var Top:Pstack; var c:char); 
Var temp:Pstack; 
Begin 
end; 
Dispose(temp); 
Temp:=Top; 
Top:=Top^.next ; 
C:=top^.ch; 
i 
next 
TOP 
K 
i 
K 
Nil 
The output: 
C 
i 
temp 
K 
TOP 
temp 
TOP 
i 
temp 
TOP 
K 
temp 
i 
K 
i 
K
Stack=Record ch: char; next: Pstack; end; 
Type Pstack=^stack; 
Var Top: Pstack; st: string[4]; i:integer; x:char; 
BEGIN 
Readln(st); top:=nil; 
For i:=1 to (length(st)-1) 
push( top, st[i] ) ; 
begin 
Pop( top, x) ; write(x,’ ’); 
end; 
END. 
Program fifi; 
While(top<>Nil) do
Is_Empty 
Function Is_Empty (top:Pstack):boolean begin if top =Nil then Is_Empty:=True; else Is_Empty:=false; end;
S_Top 
Function s_Top (top:Pstack): integer/…Type begin if not is_Empty(top) then s_Top :=top^.key; end; 
NOTE: Don’t write then : top:=top^.next 
Cause that function just return the element without change in stack!!
NOTE: 
You can reach to the data , at a determinate place by pointers without do pop() cause with pop() you cant restoring your ‘poped’ data .. You do: S:=top; {bring the third element 64} i:=s^.next^.next^.key; OR: S:=top; {bring the 11 element} For i:=1 to 10 do s:=s^.next; i:=s^.key 
52 
next 
TOP 
35 
64 
76 
Nil 
S 
+1
Homework: 
لديك تعبير حسابي مثلا : 
(( (1 * (3+2)) * 1) +3 ) 
المطلىب : 
حساب قيمة التعبير الحسابي وطباعتها على 
الشاشة باستخدام المكدس pop/push 
ملاحظة :مكدس واحد فقط ! 
+20 points
Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 
2 bytes team

2Bytesprog2 course_2014_c5_pointers

  • 1.
    Prog_2 course- 2014 2 bytes team Kinan keshkeh IT Engineering-Damascus University 3rd year
  • 2.
  • 3.
    Static Variables &&Dynamic Variables Static Variables : var x:integer; The memory for this variables x still until program End , and doesn’t free even we don’t use it Ex: X Memory We determine their places at memory first of all value
  • 4.
    Dynamic Variables : Memory The Heap X var x: ^integer; Ex: the address the address value The memory for this variables x in the Heap, we can take it or dispose it, so it can be free!! We determine their places at memory during the program
  • 5.
    Why Dynamic?? 1)better usage to memory 2) to create advance data structure like trees , lists….atc
  • 6.
    What is thepointer? The Definition The pointer is an address to a memory place Var ptr: ^Type; Lets Have an explication!! but…..
  • 7.
    P1 X Value.. P1^ P1 Var p1,p2: ^Type; x :Type; P1^ P1 P1^ X:=value P2 xx P1^ P2 New(p1); P1^:=x Value.. P2:=P1 Value.. P1:=nil; Value.. +1
  • 8.
    xx P1^ P2 Dispose(p2); Value.. xx P1^ xx P2:=nil; Value.. +1
  • 9.
    Program pointer onSoso; There is wrong ! Type pEmp=^Emp; Emp=Record eno:integer; ename:string[20]; esal:real; end; Var E:Emp; E1:pEmp; n,m,y:^integer; EXAMPLE:
  • 10.
    Begin i:=5; new(y);y^:=50; write(y); write(^y); write(y^); y:=i y^:=i ; y^:=20; i:=y^; y:=0; dispose(y); y:=nil; new(m); new(y); m^:=30; y^:=80; n:=y; write(y^); write(n^); E1:=nil; new(E1); E1^:=E ; Read(E1^.Eno); Dispose(E1); End. false false TRUE NOTE: Without “ New(n)” !! 80 80 false TRUE
  • 11.
    STACK {FILO} FirstIn Last Out pringles
  • 12.
    The Data next Nil TOP Stack=Record the Data: Type; next: Pstack; end; The Definition Pstack=^stack; Var Top: Pstack; D:Type; ……….
  • 13.
    Stack procedures : Push(s,ch) Pop(s,ch) S_Top(s,ch) Is_Empty(s) 3) 2) 1) 4)
  • 14.
    EX: You havea string “KiKi” How to put/take it in /from the STACK ?! i next TOP K i K Nil
  • 15.
    Stack=Record ch: char;next: Pstack; end; Type Pstack=^stack; Var Top: Pstack; st: string[4]; i:integer; x:char; BEGIN Readln(st); top:=nil; For i:=1 to (length(st) ) do push( top, st[i] ) ; While(top<>Nil) do begin Pop( top, x) ; write(x,’ ’); end; END. Program fifi;
  • 16.
    PUSH Procedure push(var Top:Pstack; c:char); Var temp:Pstack; Begin end; new(temp); temp^.ch:=c; temp^.next:=top; top:=temp; i next TOP K i K Nil temp K TOP i TOP K TOP i
  • 17.
    Stack=Record ch: char; next: Pstack; end; Type Pstack=^stack; Var Top: Pstack; st: string[4]; i:integer; x:char; BEGIN Readln(st); top:=nil; For i:=1 to (length(st) ) push( top, st[i] ) ; While(top<>Nil) do begin Pop( top, x) ; write(x,’ ’); end; END. Program fifi;
  • 18.
    POP Procedure pop(var Top:Pstack; var c:char); Var temp:Pstack; Begin end; Dispose(temp); Temp:=Top; Top:=Top^.next ; C:=top^.ch; i next TOP K i K Nil The output: C i temp K TOP temp TOP i temp TOP K temp i K i K
  • 19.
    Stack=Record ch: char;next: Pstack; end; Type Pstack=^stack; Var Top: Pstack; st: string[4]; i:integer; x:char; BEGIN Readln(st); top:=nil; For i:=1 to (length(st)-1) push( top, st[i] ) ; begin Pop( top, x) ; write(x,’ ’); end; END. Program fifi; While(top<>Nil) do
  • 20.
    Is_Empty Function Is_Empty(top:Pstack):boolean begin if top =Nil then Is_Empty:=True; else Is_Empty:=false; end;
  • 21.
    S_Top Function s_Top(top:Pstack): integer/…Type begin if not is_Empty(top) then s_Top :=top^.key; end; NOTE: Don’t write then : top:=top^.next Cause that function just return the element without change in stack!!
  • 22.
    NOTE: You canreach to the data , at a determinate place by pointers without do pop() cause with pop() you cant restoring your ‘poped’ data .. You do: S:=top; {bring the third element 64} i:=s^.next^.next^.key; OR: S:=top; {bring the 11 element} For i:=1 to 10 do s:=s^.next; i:=s^.key 52 next TOP 35 64 76 Nil S +1
  • 23.
    Homework: لديك تعبيرحسابي مثلا : (( (1 * (3+2)) * 1) +3 ) المطلىب : حساب قيمة التعبير الحسابي وطباعتها على الشاشة باستخدام المكدس pop/push ملاحظة :مكدس واحد فقط ! +20 points
  • 24.
    Group : grouplink Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team