You are on page 1of 55

Trng i Hc Bch Khoa TP H Ch Minh

Khoa Khoa hc & K thut My tnh

N TP V CON TR

Trn Giang Sn
tgson@cse.hcmut.edu.vn

Ni dung

Bin
Bin con tr
Cp pht b nh
Con tr ca con tr
Truyn tham s
V d truyn tham s
Mng cp pht ng

BIN
Khai bo bin
short
x;
char
c;
float
z = 5;
x = 1;
-Tn gi
-a ch
-Gi tr
-Kch thc

Addr
120

Value

Name
1

121
130
150

c
5

151
152
153

BIN CON TR
Khai bo bin
short
char
float
x = 1;
short*
float*

x;
c;
z = 5;

Addr
120

Value

Name
1

121
130
150

c
5

151
152

px;
pz;

153
200

px

201
202

(int *p, q; //q : int


int *p, *q)

203
204-207

pz
4

BIN CON TR
Khai bo bin
short
x;
char
c;
float
z = 5;
x = 1;
short* px;
float* pz;
px = &x; //gn tr
pz = &z;
(px = NULL;
px = 0;)

Addr
120

Value

Name
1

121
130
150

c
5

120

px

150

pz

151
152
153
200
201
202
203
204-207

BIN CON TR
Mt s tnh cht ca bin con tr:
Cha a ch ca mt bin khc, ch khng
cha gi tr :
px = &x;// OK
px = 120; // li
Kch thc tt c cc bin con tr u bng
nhau. Kiu con tr dng xc nh kch thc
vng nh khi thao tc:
px = px + 10;
// 10 = 10*size(short)
Thao tc hn ch
Khng th thc hin cc php cng, tr, nhn, chia
Ch thc hin c php gn, php cng vi hng s
6

CP PHT B NH
Cp pht stack:
Cp pht stack
Mun cp pht ch cn khai bo
T ng hy

Cp pht heap
Cp pht heap
Mun cp pht, dng ton t new
Hy dng ton t delete

CP PHT B NH
int
a;
int*
pa;
pa = &a;
int*
pb;

Addr

Value

100

150

100

200

Addr

pa
pb

Value

CP PHT B NH
int
a;
int*
pa;
pa = &a;
int*
pb;
pb = new int;

Addr

Value

100

150

100

pa

200

800

pb

Addr

Value

800

CP PHT B NH
C 2 cch thao
tc vng nh a:
a = 5; *pa = 5;
Ch c mt
cch thao tc
vng nh heap:
l thng qua
con tr:
*pb = 15

Addr

Value

100

150

100

pa

200

800

pb

Addr
800

Value
15

10

CP PHT B NH

11

CON TR CA CON TR
int** p2;
int* p1;
int
a = 10;

Addr

Value

100

p2

150

p1

200

10

printf("%d\n", a);
p1 = &a;
p2 = &p1;
*p1 = 15;
printf("%d\n", a);
**p2 = 20;
printf("%d\n", a);
12

CON TR CA CON TR
int** p2;
int* p1;
int
a = 10;

Addr

Value

100

150

p2

150

200

p1

200

10

printf("%d\n", a);
p1 = &a;
p2 = &p1;
*p1 = 15;
printf("%d\n", a);
**p2 = 20;
printf("%d\n", a);
13

CON TR CA CON TR
int** p2;
int* p1;
int
a = 10;

Addr

Value

100

150

p2

150

200

p1

200

15

printf("%d\n", a);
p1 = &a;
p2 = &p1;
*p1 = 15;
printf("%d\n", a);
**p2 = 20;
printf("%d\n", a);
14

CON TR CA CON TR
int** p2;
int* p1;
int
a = 10;

Addr

Value

100

150

p2

150

200

p1

200

20

printf("%d\n", a);
p1 = &a;
p2 = &p1;
*p1 = 15;
printf("%d\n", a);
**p2 = 20;
printf("%d\n", a);
15

CON TR CA CON TR
Con tr cp pht
ng
int** p2;
int
a;

Addr

Value

100

p2

200

p2 = new int*;
*p2 = &a;
**p2 = 10;

Addr

Value

printf("%d\n", a);
16

CON TR CA CON TR
Con tr cp pht
ng
int** p2;
int
a;

Addr

Value

100

800

200

p2
a

p2 = new int*;
*p2 = &a;
**p2 = 10;

Addr

Value

800

printf("%d\n", a);
17

CON TR CA CON TR
Con tr cp pht ng
int** p2;
int
a;

Addr

Value

100

800

200

p2
a

p2 = new int*;
*p2 = &a; //p2 = 10 err
**p2 = 10;

Addr
800

Value
200

printf("%d\n", a);
18

CON TR CA CON TR
Con tr cp pht ng
int** p2;
int
a;

Addr

Value

100

800

p2

200

10

p2 = new int*;
*p2 = &a; //p2 = 10 err
**p2 = 10;

Addr
800

Value
200

printf("%d\n", a);
19

CON TR CA CON TR
Con tr cp pht ng:
int**
p2;

Addr

Value

100

800

p2

p2 = new int*;

*p2 = new int;


**p2 = 10;

Addr

Value

800

printf("%d\n", **p2);
20

CON TR CA CON TR
Con tr cp pht ng:
int**
p2;

Addr

Value

100

800

p2

p2 = new int*;

*p2 = new int;


**p2 = 10;

Addr
800

Value
900

900

printf("%d\n", **p2);
21

CON TR CA CON TR
Con tr cp pht ng:
int**
p2;

Addr

Value

100

800

p2

p2 = new int*;

*p2 = new int;


**p2 = 10;

Addr

Value

800

900

900

10

printf("%d\n", **p2);
22

TRUYN THAM S
Hm l khi lnh c nhn tham s v tr v
kt qu
<accessType> <returnType>
i1
FunctionName (<parameters>)
o
i2
{ ... }

Ba cch truyn tham s:


Truyn theo kiu gi tr
Truyn theo kiu tham kho
Truyn theo kiu tr-kt qu
23

TRUYN THAM S
Truyn theo gi tr

Gi tr ca tham s thc c sao chp vo thng s


hnh thc
Mi s thay i gi tr tham s hnh thc khng nh
hng n tham s thc (gi tr gc)

void Increment(int Number)


{
Number += 1;

Number

}
int
a = 10;
Increment(a); //a = 10
24

TRUYN THAM S
Truyn theo kiu tham kho
Gi tr ca tham s thc c sao chp vo
thng s hnh thc
S thay i gi tr tham s hnh thc nh hng
n tham s thc (gi tr gc)

void Increment1(int & Number)


{
Number += 1;

Number

}
int
a = 10;
Increment1(a); //a = 11
25

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}
26

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1,*p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr
100

Addr

Value
600

p1

Value

600

27

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr
100

Value
600

Addr
600

p1

Value
10

28

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

Addr
600

Value
10

700

29

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

Addr

Value

600

10

700

20

30

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

304

700

308

600

Addr

Value

600

10

700

20

31

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

600

304

700

308

600

Addr

Value

600

10

700

20

32

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

600

304

700

308

700

Addr

Value

600

10

700

20

33

V D 1
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

600

304

600

308

700

Addr

Value

600

10

700

20

34

V D 2
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* &a, int* &b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

304

308

Addr

Value

600

10

700

20

35

V D 2
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* &a, int* &b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

600

304

308

Addr

Value

600

10

700

20

36

V D 2
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* &a, int* &b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

700

p1

200

700

p2

300

600

304

308

Addr

Value

600

10

700

20

37

V D 2
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* &a, int* &b){
int *t;
t = a;
a = b;
b = t;
}

Addr

Value

100

700

p1

200

600

p2

300

600

304

308

Addr

Value

600

10

700

20

38

V D 3
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(&p1, &p2);
printf(%d %d, *p1, *p2);
}
void func(int **a, int **b){
int *t;
t = *a;
*a = *b;
*b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

304

200

308

100

Addr

Value

600

10

700

20

39

V D 3
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(&p1, &p2);
printf(%d %d, *p1, *p2);
}
void func(int **a, int **b){
int *t;
t = *a;
*a = *b;
*b = t;
}

Addr

Value

100

600

p1

200

700

p2

300

600

304

200

308

100

Addr

Value

600

10

700

20

40

V D 3
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(&p1, &p2);
printf(%d %d, *p1, *p2);
}
void func(int **a, int **b){
int *t;
t = *a;
*a = *b;
*b = t;
}

Addr

Value

100

700

p1

200

700

p2

300

600

304

200

308

100

Addr

Value

600

10

700

20

41

V D 3
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(&p1, &p2);
printf(%d %d, *p1, *p2);
}
void func(int **a, int **b){
int *t;
t = *a;
*a = *b;
*b = t;
}

Addr

Value

100

700

p1

200

600

p2

300

600

304

200

308

100

Addr

Value

600

10

700

20

42

MNG CP PHT NG
int* p;
p = new int[10];
*p = 25; //gn 25 cho phn t u tin p*0+
p++;
*p = 35;// gn 35 cho phn t th 2, p*1+
int list[5] = {5, 10, 15, 20, 25};
p = list;
cout << p[2];
p[1] = 7;
cout << p[1];
43

MNG CP PHT NG
int* intList;
int arraySize;
cout << Enter array size: ;
cin >> arraySize;
intList = new int[arraySize];

44

Shallow, Deep Copy


int* first;
int* second;
first = new int[10];

45

Shallow, Deep Copy


second = first;

delete[] second;

46

Shallow, Deep Copy


second = new int[10];
for(int i = 0; i<10; i++)
second[i] = first[i];

47

Con tr hm
float Square(float x){
return(x*x);
}
float Cubic(float x){
return(x*x*x);
}
float Max(float (*f)(float),float (*g)(float),float x){
return ((*f)(x)>(*g)(x)?(*f)(x):(*g)(x));
}
48

Con tr hm
void main(){
float x;
printf("\nInput x=");
scanf("%f",&x);
printf("\nSquare of x=%f\tis %f ",x, Square(x));
printf("\nCubic of x=%f\tis %f \r\n",x, Cubic(x));
printf("\nMax of {x*x,x*x*x} = %f\r\n",
Max(Square, Cubic,x ));
}
49

Con tr hm

50

Bi tp
1) Cho bit kt xut ca chng trnh sau
void main(){
float x, y, *p, *q, *r;
x = 100;
q = &y;
p = &x;
r = q;
*r = x * 2;
cout << x << <<y << \n;
cout << p << <<q << << r <<\n;
}
51

Bi tp
2) Gi s mt chng trnh bt u vi nhng
khai bo sau:
int i = 10;
int* pi = &i;
int** ppi = &pi;
int*** pppi = &ppi;
Hy vit bn lnh gn khc nhau tng gi
tr ca i ln 1.
52

Bi tp
3) Cho bit kt xut ca chng trnh sau
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf(%d %d, *p1, *p2);
}
void func(int* a, int* &b){
int *t;
t = a;
a = b;
b = t;
}
53

C THM
Essential C++, Stanley B. Lippman
Pointers Allow for Flexibility (p23)

C++ Primer Plus, Steven Prata


Chapter 4. COMPOUND TYPES

54

CU HI?

55

You might also like