Anda di halaman 1dari 27

Con tr

GV. Nguyn Minh Huy

K thut lp trnh - Nguyn Minh Huy 1


Ni dung
 Khi nim con tr
tr..
 S dng con tr
tr..
 Con tr vs. mng.
mng.

K thut lp trnh - Nguyn Minh Huy 2


Ni dung
 Khi nim con tr.
tr.
 S dng con tr
tr..
 Con tr vs. mng.
mng.

K thut lp trnh - Nguyn Minh Huy 3


Khi nim con tr
 B nh my tnh
tnh::
 RAM (R
(Random Access Memory): RAM (4GB)
0
 Primary vs. Secondary memory.
 RAM dng cha
cha::
 H iu hnh
hnh..
 Chng trnh
trnh:: lnh + d liu
liu..

 Bao gm cc nh 1 byte.
 RAM 4GB ~ 4 t nh.
nh.
 Mi nh c a ch nh s t 0.
 RAM 4GB a ch t 0  232 1.
232

K thut lp trnh - Nguyn Minh Huy 4


Khi nim con tr
 a ch bin
bin::
 iu g xy ra khi khai bo bin
bin?
?
 Cp mt dy nh lin tip
tip.. int x;
 Gn tn bin vi a ch u dy
dy..
65 66 67 68
 Bao nhiu ?  kiu d liu
liu.. x ? ? ? ?
 a ch bin = a ch u tin
tin..
 Gi tr bin c lu th no
no??
 Chia gi tr bin thnh cc byte. x = 1057;
 Lu mi byte vo mt nh.
nh. 65 66 67 68
 Th t lu:
lu: x 33 4 0 0
 Byte thp n cao
cao..
 u n cui dy
dy..

K thut lp trnh - Nguyn Minh Huy 5


Khi nim con tr
 Kiu a ch trong C:
 Mi bin c mt a ch
ch..
 a ch bin c kiu
kiu:: <
<kiu
kiu d liu bin
bin>
> *.
 Bin int c a ch kiu int *.
 Ton t &:
 Cng dng
dng:: ly a ch ca bin
bin..
 C php
php:: &<Tn bin
bin>;
>; x = 1057;
int x = 1057;
65 66 67 68
float y = 1.25;
x 33 4 0 0
int *address_x = &x;
float *address_y
*address_y = &y; 91 92 93 94
address_x 65 0 0 0

K thut lp trnh - Nguyn Minh Huy 6


Khi nim con tr
 Con tr trong C:
 L bin c kiu a ch
ch..
 Lu a ch ca bin khc
khc..
 Kch thc con tr
tr::
 Bng nhau d khc kiu a chch..
 Bng kch thc s nguyn int.
 Ty thuc vo h my tnh
tnh..
V d:
d:
- H my 16-
16-bit, 2 bytes.
- H my 32-
32-bit, 4 bytes.

K thut lp trnh - Nguyn Minh Huy 7


Ni dung
 Khi nim con tr
tr..
 S dng con tr.
tr.
 Con tr vs. mng.
mng.

K thut lp trnh - Nguyn Minh Huy 8


S dng con tr
 Khai bo con tr
tr::
 Khai bo bin c kiu a ch
ch..
 Cch 1:
<Kiu d liu
liu>
> *<Tn con tr
tr>;
>;
int *p1; // Con tr kiu int.
float *p2; // Con tr kiu float.
 Cch 2:
typedef <Kiu d liu
liu>
> * <Tn thay th>;
th>;
<Tn thay th>
th> <
<Tn
Tn con tr
tr>;
>;
typedef int * ConTroInt
ConTroInt;;
typedef float * ConTroFloat
ConTroFloat;;
ConTroInt p1;
ConTroFloat p2;

K thut lp trnh - Nguyn Minh Huy 9


S dng con tr
 Khi to con tr
tr::
 Con tr va khai bo nhn a ch no
no? ?
 Ton t &: khi to a ch cho con tr
tr..
<Tn con tr
tr>
> = &<Tn bin
bin>;
>;
int x;
int *p = &x;
 Con tr kiu g th ch nhn a ch ca bin kiu !!
!!
float y;
int *q = &y; // Sai.
Sai.
 a ch NULL:
 a ch rng
rng,, khng thuc nh no
no..
 Dng khi to a ch mc nh cho con tr
tr..
int *r = NULL
NULL;; // r nhn a ch rng
rng..

K thut lp trnh - Nguyn Minh Huy 10


S dng con tr
 Truy xut ni dung vng nh
nh::
 Ton t *:
 Cng dng
dng:: truy xut ni dung vng nh con tr gi a ch
ch..
 C php
php:: <
<Tn
Tn bin
bin> > = *<Tn con trtr>;
>;
int x = 5;
int *p = &x;
int k = *p;
*p; // ly gi tr ca x.
printf(%d
printf (%d\\n, p); // Xut a ch x.
printf(%d
printf (%d\\n, *p);
*p); // Xut gi tr x.
printf(%d
printf (%d\\n, &p);
&p); // Xut a ch p.
 Con tr tr
tr n vng nh n gi a ch
ch!!
!!
72 73 74 75 91 92 93 94
x 5 0 0 0 p 72 0 0 0

K thut lp trnh - Nguyn Minh Huy 11


S dng con tr
 Truyn tham s con tr
tr:: void foo
foo(( int *g )
{
 Truyn tham tr:
tr: *g = *g + 1;
 Truyn bn sao con tr vo hm
hm.. g = g + 1;
 Gi tr con tr KHNG thay i
i.. }
 Ni dung vng nh con tr tr
tr n void main
main()
()
C TH b thay i
i.. {
int x = 5;
int *p = &x;
main()
72 73 74 75 foo(int *g) foo(
foo(p);
x 5 0 0 0 66 67 68 69 foo(
foo(&x);
&x);
g 72 0 0 0 // Gi tr x i.
i.
}
91 92 93 94
p 72 0 0 0

K thut lp trnh - Nguyn Minh Huy 12


S dng con tr
 Truyn tham s con tr
tr:: void foo
foo(( int *&g
*&g )
{
 Truyn tham chiu
chiu:: *g = *g + 1;
 Truyn bn gc con tr vo hm
hm.. g=g+1
 Gi tr con tr C TH thay i
i.. }
 Ni dung vng nh con tr tr
tr n void main
main()
()
C TH b thay i
i.. {
int x = 5;
int *p = &x;
main()
72 73 74 75 foo(
foo(p);
x 5 0 0 0 foo(
foo(&x);
&x); // Sai
// Gi tr x i.
i.
91 92 93 94 foo(int *&g) // Gi tr p i
i..
p 72 0 0 0 g }

K thut lp trnh - Nguyn Minh Huy 13


S dng con tr
 Con tr cu trc
trc::
 Gi a ch bin cu trc
trc..
 Khai bo
bo::
 Cch 1: < <Kiu
Kiu cu trc
trc>> *<Tn con tr
tr>;
>;
 Cch 2: typedef <Kiu cu trc trc>
> * <Tn thay th>;
th>;
<Tn thay th>
th> <<Tn
Tn con tr
tr>;
>;
struct PhanSo
{
int tu
tu,, mau;
mau;
};
typedef PhanSo * ConTroPhanSo
ConTroPhanSo;;

PhanSo *p;
ConTroPhanSo q;

K thut lp trnh - Nguyn Minh Huy 14


S dng con tr
 Con tr cu trc
trc::
 Truy xut thnh phn
phn::
 Cch 1: (*<
(*<Tn con tr
tr>
>).<
.<Tn
Tn thnh phn
phn>;
>;
 Cch 2: <
<Tn
Tn con tr
tr>>-><Tn thnh phn
phn>;
>;
PhanSo p;
ConTroPhanSo q = &p;

(*q
(*q).tu = 1;
q->mau = 2;

K thut lp trnh - Nguyn Minh Huy 15


Ni dung
 Khi nim con tr
tr..
 S dng con tr
tr..
 Con tr vs. mng.
mng.

K thut lp trnh - Nguyn Minh Huy 16


Con tr vs. mng
 Mng trong C:
 L mt con tr
tr..
 Gi a ch phn t u tin
tin..
void main
main()()
{
int a[ 10 ];
printf(%d
printf (%d\\n, a);
printf(%d
printf (%d\\n, &a[0]
&a[0]);
); // a == &a[0].
}
a[0] a[1] a[2]
72 73 74 75 76 77 78 79 80 81 82 83
a 1 0 0 0 2 0 0 0 3 0 0 0

K thut lp trnh - Nguyn Minh Huy 17


Con tr vs. mng
 Con tr n mng
mng::
 Truy xut mng gin tip
tip..
 Xt on chng trnh sau:
sau:
int a[100] = { 1, 2, 3 };
int *p = a;
a;
*p = *p + 1;
printf(%d
printf (%d\\n, *p);
 C ch hot ng
ng::
a[0] a[1] a[2]
72 73 74 75 76 77 78 79 80 81 82 83
a 1 0 0 0 2 0 0 0 3 0 0 0

44 45 46 47
p 72 0 0 0

K thut lp trnh - Nguyn Minh Huy 18


Con tr vs. mng
 Php ton tng
tng,, gim con tr
tr::
 Gi tr con tr tng gim theo kch thc kiu d liu
liu..
 Cng thc
thc::
 <Con trtr>
> +/
+/-- k = <a
<a ch
ch>> +/
+/-- k * sizeof(<
sizeof(<Kiu
Kiu d liu
liu>).
>).
int a[100] = { 1, 2, 3 };
int *p = a;a;
printf(%d
printf (%d\\n, p + 1);
1);
printf(%d
printf (%d\\n, *(p + 2) );
a[0] a[1] a[2]
72 73 74 75 76 77 78 79 80 81 82 83
a 1 0 0 0 2 0 0 0 3 0 0 0

44 45 46 47 p+1 p+2
p 72 0 0 0

K thut lp trnh - Nguyn Minh Huy 19


Con tr vs. mng
 Ton t [ ]:
 Truy xut ni dung vng nh con tr gi a ch
ch..
 Cng thc
thc::
<Con tr
tr>[<
>[<Ch
Ch s>]
s>] ~ * (<Con tr
tr>> + <Ch
<Ch s>)
s>)
int a[100] = { 1, 2, 3 };
int *p = a;

a[2] = 5;
*(a + 2) = 5;
*(p + 2) = 5;
p[2] = 5;

K thut lp trnh - Nguyn Minh Huy 20


Con tr vs. mng
 Truyn tham s mng
mng::
 Khng phi truyn tt c mng
mng..
 Ch truyn a ch phn t u tin
tin..
 Truyn con tr n phn t u tin
tin..
void xuatMang
xuatMang((int a[ ], ], int n) {
for (int
(int i = 0; i < n; i++)
printf(%d
printf (%d , *(a++) ); // Tng t a[ i ]
}
void main
main()() {
int a[100];
xuatMang((a, 100);
xuatMang
for (int
(int i = 0; i < n; i++)
printf(%d
printf (%d , *(a++) ); // Sai.
Sai.
}

K thut lp trnh - Nguyn Minh Huy 21


Tm tt
 Khi nim con tr
tr::
 Bin c kiu l a ch
ch..
 S dng con tr
tr::
 Khai bo
bo:: <
<Kiu
Kiu d liu
liu>
> *.
 Khi to:
to: ton t & ly a ch bin
bin..
 Ton t *: truy xut ni dung vng nh
nh..
 Con tr vs. mng:
mng:
 Mng trong C l mt con tr tr..
 Con tr c th tr
tr n vng nh mng
mng..
 Ton t [ ]: truy ni dung vng nh mng
mng..

K thut lp trnh - Nguyn Minh Huy 22


Bi tp
 Bi tp 2.1:
Cho on chng trnh sau:
sau:
void main()
{
int *x, y = 2;
float *z = &y;

*x = *z + y;
printf(%d,
printf (%d, y);
}
a) on chng trnh trn c li g
g??
b) Hy sa li cho ng
ng..

K thut lp trnh - Nguyn Minh Huy 23


Bi tp
 Bi tp 2.2:
Cho on chng trnh sau:
sau:
void main()
{
double m[100];
double *p1, *p2;

p1 = m;
p2 = &m[6];
}
Hy cho bit p1 cch p2 bao nhiu byte?

K thut lp trnh - Nguyn Minh Huy 24


Bi tp
 Bi tp 2.3:
Hy gii thch s khc nhau gia 3 hm sau
sau::
void swap1( int x, int y ) void swap3( int *x, int *y )
{ {
int temp = x; int temp = *x;
x = y; *x = *y;
y = temp; *y = temp;
} }

void swap2( int &x, int &y )


{
int temp = x;
x = y;
y = temp;
}

K thut lp trnh - Nguyn Minh Huy 25


Bi tp
 Bi tp 2.4:
Hy cho bit kt qu xut ra mn hnh ca on chng trnh sau:
sau:
#include <stdio.h
<stdio.h>>

void main()
{
int x = 1023;
char *p = (char *)&x;

printf(%d
printf (%d %d %d %d
%d\\n, p[0], p[1], p[2], p[3]);
}

K thut lp trnh - Nguyn Minh Huy 26


Bi tp
 Bi tp 2.5:
Vit chng trnh C s dng con tr thc hin
hin::
a) Nhp mng N phn s t bn phm
phm..
b) Trch ra mng cc phn s m.
m.
c) Xut kt qu ra mn hnh.

K thut lp trnh - Nguyn Minh Huy 27