Vous êtes sur la page 1sur 19

Tin Hc C S 4

Con tr

Ni Dung

Khai bo
Cc ton t &, *, =, +
Nhc li v truyn tham s a ch
Con tr & mng
Cp pht vng nh ng

L Nguyn Khi

Gii thiu v Con Tr


nh ngha Con tr:

Kiu d liu lu tr a ch ca cc vng d liu trong


b nh my tnh
Cha a ch ca bin mang gi tr c th

Mc ch Con tr:

Truyn tham s kiu a ch


Biu din cc kiu & cu trc d liu ng
Lin quan cht ch n mng & xu

Con tr c s dng trong hm scanf


L Nguyn Khi

Bin Con Tr
Con tr l kiu d liu

Kiu con tr phi c nh ngha trn mt kiu c


s c nh ngha trc .
typedef kiucs *Tnkiu;
typedef
int
* IntPointer;
// IntPointer l kiu con tr - a ch vng nh kiu int

C th dng bin lu tr con tr


Khng phi kiu int, double,

L con tr ch n bin kiu d liu int, double,


L Nguyn Khi

Bin Con Tr - V d
int x;
int *p;
IntPointer p1;
p v p1 c khai bo l con tr ch n kiu int
p v p1 l bin kiu int*
p v p1 c th dng lu a ch ca bin kiu int
C th s dng p v p1 lu a ch ca x

L Nguyn Khi

Khai bo bin Con Tr


Khai bo bin con tr ging cc bin kiu
d liu khc:
Thm ton t * vo trc bin
To ra con tr ch n kiu d liu khai bo

Ton t * bt buc phi trc bin


int *p1, *p2, v1, v2;

p1, p2 l con tr ch n bin kiu int


v1, v2 l bin kiu int
L Nguyn Khi

Khai bo Con Tr trong C


int
int

*pi;
*pi;

long
long int
int *p;
*p;
float
float
char
char

*pf;
*pf;
c,
c, d,
d, *pc;
*pc; /*
/* cc v
v dd kiu
kiu char
char
pc
pc l
l con
con tr
tr n
n char
char */
*/

double
double *pd,
*pd, e,
e, f;
f;
char
char

/*
/* pd
pd l
l
ee and
and

con
con tr
tr n
n double
double
ff are
are double
double */
*/

*start,
*start, *end;
*end;
L Nguyn Khi

Ton t &
&: ton t ly (tr v) a ch ca 1 bin.
a ch ca tt c cc bin trong chng trnh u
c ch nh t khi khai bo.
char
char gg == 'z';
'z';
int
int main()
main()
{{
char
char cc == 'a';
'a';
char
char *p;
*p;
pp == &c;
&c;
pp == &g;
&g;
return
return 0;
0;
}}
L Nguyn Khi

p
0x1132
p
0x91A2

c
0x1132

'a'

g
0x91A2

'z'

Ton t *

*: ton t truy xut gi tr ca vng nh c qun l


bi con tr.
#include
#include <stdio.h>
<stdio.h>
char
char gg == 'z';
'z';
int
int main()
main()
{{
char
char cc == 'a';
'a';
char
char *p;
*p;
pp == &c;
&c;
printf("%c\n",
printf("%c\n",
pp == &g;
&g;
printf("%c\n",
printf("%c\n",
return
return 0;
0;
}}
L Nguyn Khi

aa
zz

*p);
*p);
*p);
*p);

p
0x1132
p
0x91A2

c
0x1132

'a'

g
0x91A2

'z'

xut gi tr ti vng nh
do p ang qun l
9

Truyn tham s a ch
#include
#include <stdio.h>
<stdio.h>
void
void ThayDoiTruyenDiaChi(int
ThayDoiTruyenDiaChi(int *v);
*v);
int
intmain()
main()
{{
int
int var
var == 5;
5;
ThayDoiTruyenDiaChi(&var);
ThayDoiTruyenDiaChi(&var);
printf("main:
printf("main: var
var == %i\n",
%i\n", var);
var);
return
return 0;
0;
}}
void
void ThayDoiTruyenDiaChi(int
ThayDoiTruyenDiaChi(int *v)
*v)
{{
(*v)
(*v) *=
*= 100;
100;
printf(ham:
printf(ham: *v
*v == %i\n",
%i\n", (*v));
(*v));
}}
L Nguyn Khi

10

Truyn tham s gi tr
#include
#include <stdio.h>
<stdio.h>
void
void ThayDoiTruyenGiaTri(int
ThayDoiTruyenGiaTri(int v);
v);
int
intmain()
main()
{{
int
int var
var == 5;
5;
ThayDoiTruyenGiaTri(var);
ThayDoiTruyenGiaTri(var);
printf("main:
printf("main: var
var == %i\n",
%i\n", var);
var);
return
return 0;
0;
}}
void
void ThayDoiTruyenGiaTri(int
ThayDoiTruyenGiaTri(int v)
v)
{{
vv *=
*= 100;
100;
printf(ham:
printf(ham: vv == %i\n",
%i\n", v);
v);
}}
L Nguyn Khi

11

Con Tr NULL
Gi tr c bit ch rng con tr khng qun l
vng no. Gi tr ny thng c dng ch
mt con tr khng hp l.
#include
#include <stdio.h>
<stdio.h>
int
int main()
main()
{{
int
int ii == 13;
13;
short
short *p
*p == NULL;
NULL;
if
if (p
(p ==
== NULL)
NULL)
printf(Con
printf(Con tr
tr
else
else
printf(Gi
printf(Gi tr
tr
return
return 0;
0;
L Nguyn Khi
}}

khng
khng hp
hp l!\n");
l!\n");
:: %hi\n",
%hi\n", *p);
*p);
12

Ton t gn =
C th gn gi tr cho bin con tr
int *p, *q;
p = q;
Gn gi tr con tr cho con tr khc
Cho p ch ti vng nh m q ang ch

Khng c nhm ln vi:

*p = *q;
Gn gi tr m q ang ch n cho bin (vng
nh) m p ang ch n
L Nguyn Khi

13

Ton t gn = - V d
C s khc bit rt quan trng khi thc hin cc php gn:
int
int
int
int
int
int

ii == 10,
10, jj == 14;
14;
*p
*p == &i;
&i;
*q
*q == &j;
&j;

*p
*p == *q;
*q;

int
int
int
int
int
int

ii == 10,
10, jj == 14;
14;
*p
*p == &i;
&i;
*q
*q == &j;
&j;

pp == q;
q; Khi
L Nguyn

p
0x15A0
q
0x15A4

p 0x15A4
0x15A0
q
0x15A4

i
10 14
0x15A0
j
14
0x15A4
i
0x15A0
j

10
14

0x15A4

14

Luyn tp in vo trng
int
int main(void)
main(void)
{{
int
int ii == 10,
10, jj == 14,
14, k;
k;
int
int *p
*p == &i;
&i;
int
int *q
*q == &j;
&j;
*p
*p +=
+= 1;
1;
pp == &k;
&k;
*p
*p == *q;
*q;
pp == q;
q;
*p
*p == i;
i;
}}

return
return 0;
0;
L Nguyn Khi

i
0x2100

10
11

j
0x2104

14

k
0x1208

14
?

p
0x120B

0x2100
0x2108

q
0x1210

0x2104

15

Con Tr & Mng


Bin kiu mng l a ch tnh ca mt vng nh, c
xc nh khi khai bo, khng thay i bao gi thay i.
Bin con tr l a ch ng ca mt vng nh, c xc
nh qua php gn a ch khi chng trnh thc thi.
#include
#include <stdio.h>
<stdio.h>
int
int main()
main()
{{
int
int a[10]
a[10] == {1,
{1, 3,
3, 4,
4, 2,
2,
int
int *p;
*p;
pp == a;
a; //a
//a == p:
p: sai
sai
printf(0x%04X
printf(0x%04X %i
%i 0x%04X
0x%04X
a,
a, a[0],
a[0], p,
p, *p);
*p);
return
return 0;
0;
L}}
Nguyn Khi

0};
0};
%i\n,
%i\n,
16

Ton t + vi s nguyn
#include
#include <stdio.h>
<stdio.h>
int
intmain()
main()
{{
short
short a[10]
a[10] == {1,
{1, 3,
3, 5,
5, 2,
2, 0};
0};
short
short *p
*p == a;
a;
printf(0x%04X
printf(0x%04X %i
%i 0x%04X
0x%04X %i\n,
%i\n,
a,
a, a[0],
a[0], p,
p, *p);
*p);
pp ++;
++;
printf(0x%04X
printf(0x%04X %i
%i 0x%04X
0x%04X %i\n,
%i\n,
a,
a, a[0],
a[0], p,
p, *p);
*p);
(*p)
(*p) ++;
++;
printf(0x%04X
printf(0x%04X %i
%i 0x%04X
0x%04X %i\n,
%i\n,
a,
a, a[0],
a[0], p,
p, *p);
*p);
return
return 0;
0;
L
Nguyn
Khi
}}

0x15A0
a
1
4
3
5
2
0

0x16B2
p
0x15A2
17

Con Tr - Luyn tp
#include
#include <stdio.h>
<stdio.h>
int
intmain()
main()
{{
int
int a[10]
a[10] == {2,
{2, 3,
3, 5,
5, 1,
1, 4,
4, 7,
7, 0};
0};
int
int *p
*p == a;
a;
printf(%i
printf(%i %i\n,
%i\n, a[0],
a[0], *p);
*p);
pp ++;
++;
printf(%i
printf(%i %i\n,
%i\n, *p,
*p, p[2]);
p[2]);
pp ++;
++; a[2]
a[2] == 9;
9;
printf(%i
printf(%i %i\n,
%i\n, p[1],
p[1], *p);
*p);
pp -=
-= 2;
2;
printf(%i
printf(%i %i\n,
%i\n, p[3],
p[3], p[1]);
p[1]);
return
return 0;
0;
}}
L Nguyn Khi

22 22
33 11
11 99
11 33

18

Cp pht vng nh ng
C th ch nh vng mi cho 1 con tr qun l bng cc
lnh hm malloc.
Vng nh do lp trnh vin ch nh phi c gii phng
bng lnh free.
#include
#include <stdio.h>
<stdio.h>
int
int main()
main()
{{
int
int *p
*p == (int*)
(int*) malloc
malloc (sizeof(int)
(sizeof(int) ** 10);
10);
p[0]
p[0] == 1;
1;
p[3]
p[3] == -7;
-7;
free(p);
free(p);
return
return 0;
0;
}}
L Nguyn Khi

19