7/30/2019 Chapter05 Pointer String
1/77
2004 Trn Minh Chu. FOTECH. VNU
1
Chng 5.
Ngn ng lp trnh C++
Chng 5 Con tr v Xu k t
7/30/2019 Chapter05 Pointer String
2/77
2004 Trn Minh Chu. FOTECH. VNU
2
Chng 5.
Chng 5 Con tr v Xu k t
mc
5.1 Gii thiu
5.2 Khai bo v khi to bin con tr5.3 Cc thao tc trn con tr5.4 Gi hm bng tham chiu5.5 S dng const vi con tr5.6 Sp xp ni bt s dng Pass-by-Reference
5.7 Cc php ton trn con tr5.8 Quan h gia con tr v mng5.9 Mng con tr5.10 V d: gi lp tro v chia bi5.11 Con tr ti hm
5.12 Gii thiu v x l k t v xu5.12.1 Tng qut v k t v xu5.12.2 Cc hm x l xu
7/30/2019 Chapter05 Pointer String
3/77
2004 Trn Minh Chu. FOTECH. VNU
3
Chng 5.
5.1 Gii thiu
Con tr (Pointer) Mnh, nhng kh lm ch
C tc dng nh truyn tham chiu (pass-by-reference) C lin quan cht chn mng v xu
Bin con tr (Pointer variable)
Cha a ch vng nhthay v cha gi tr Thng thng, bin cha gi tr (tham chiu trc tip)
Con tr cha a ch ca bin mang gi trc th (tham chiu gin tip)
count
7
countPtr
count
7
7/30/2019 Chapter05 Pointer String
4/77
2004 Trn Minh Chu. FOTECH. VNU
4
Chng 5.
5.2 Khai bo v khi to bin con tr
Khai bo con tr * cho bit bin l con tr
int *myPtr;d liu kiu int c a ch lmyPtr, con tr kiu int *
Mi con tr cn mt du saoint *myPtr1, *myPtr2;
C th khai bo con tr ti bt c kiu d liu no
Khi to con tr (Pointer initialization) Khi to v0,NULL, hoc a ch
0 hocNULL khng trn u c
7/30/2019 Chapter05 Pointer String
5/77
2004 Trn Minh Chu. FOTECH. VNU
5
Chng 5.
5.3 Cc thao tc i vi con tr
& Ton ta ch (address operator) Tr va ch vng nhca ton hng
V dint y = 5;int *yPtr;yPtr = &y; // yPtr cha a ch ca y
yPtr trn y
yPtr
y
5
yptr
12FEA8 12FED4
y
12FED4 5
a ch ca y lgi tr ca yptr
7/30/2019 Chapter05 Pointer String
6/77
2004 Trn Minh Chu. FOTECH. VNU
6
Chng 5.
5.3 Cc thao tc i vi con tr
*php thm nhp (indirection/dereferencing) Tr vi tng m con tr tr ti
*yPtr tr v y (v yPtr trn y). con tr khi b thm nhp (dereferenced) l gi tr tri (lvalue)
*yptr = 9; // assigns 9 to y
* v & ngc nhau
7/30/2019 Chapter05 Pointer String
7/77
2004 Trn Minh Chu.FOTECH. VNU.
7
fig05_04.cpp
(1 of 2)
1 // Fig. 5.4: fig05_04.cpp
2 // Using the & and * operators.
3 #include
4
5 using std::cout;
6 using std::endl;
7
8 intmain()
9 {
10 int a; // a is an integer
11 int *aPtr; // aPtr is a pointer to an integer
12
13 a = 7;
14 aPtr = &a; // aPtr assigned address of a15
16 cout
7/30/2019 Chapter05 Pointer String
8/77
2004 Trn Minh Chu.FOTECH. VNU.
8
fig05_04.cpp
(2 of 2)
fig05_04.cpp
output (1 of 1)
26 return 0; // indicates successful termination
27
28 } // end main
The address of a is 0012FED4
The value of aPtr is 0012FED4
The value of a is 7
The value of *aPtr is 7
Showing that * and & are inverses of each other.
&*aPtr = 0012FED4
*&aPtr = 0012FED4* v & ngc nhau; cng kt qu khicng s dng c 2 vi aPtr
7/30/2019 Chapter05 Pointer String
9/77
2004 Trn Minh Chu. FOTECH. VNU
9
Chng 5.
5.4 Gi hm bng tham chiu
3 cch truyn tham s cho hm Truyn gi tr (Pass-by-value)
Truyn tham chiu vi i s l tham chiu (Pass-by-reference with reference arguments)
Truyn tham chiu vi i s l con tr (Pass-by-reference withpointer arguments)
7/30/2019 Chapter05 Pointer String
10/77
7/30/2019 Chapter05 Pointer String
11/77
7/30/2019 Chapter05 Pointer String
12/77
2004 Trn Minh Chu.FOTECH. VNU.
12
fig05_06.cpp
(2 of 2)
fig05_06.cpp
output (1 of 1)
25 // calculate and return cube of integer argument
26 int cubeByValue( int n )
27 {
28 return n * n * n; // cube local variable n and return result
29
30 } // end function cubeByValue
The original value of number is 5
The new value of number is 125
cubeByValue nhn thamspassed-by-value
Tnh lp phng v tr vbina phng (local variable) n
7/30/2019 Chapter05 Pointer String
13/77
2004 Trn Minh Chu.FOTECH. VNU.
13
fig05_07.cpp
(1 of 2)
1 // Fig. 5.7: fig05_07.cpp
2 // Cube a variable using pass-by-reference
3 // with a pointer argument.
4 #include
5
6 using std::cout;
7 using std::endl;
8
9 voidcubeByReference( int * ); // prototype10
11 intmain()
12 {
13 int number = 5;
1415 cout
7/30/2019 Chapter05 Pointer String
14/77
2004 Trn Minh Chu.FOTECH. VNU.
14
fig05_07.cpp
(2 of 2)
fig05_07.cpp
output (1 of 1)
26 // calculate cube of *nPtr; modifies variable number in main
27 voidcubeByReference( int *nPtr )
28 {
29 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr
30
31 } // end function cubeByReference
The original value of number is 5
The new value of number is 125
cubeByReference nhn a chca bin kiu int,tc l con tr trn mt s int
Thay i v truy nhp binkiu int s dng ton tthm nhp *
7/30/2019 Chapter05 Pointer String
15/77
7/30/2019 Chapter05 Pointer String
16/77
7/30/2019 Chapter05 Pointer String
17/77
7/30/2019 Chapter05 Pointer String
18/77
2004 Trn Minh Chu.FOTECH. VNU.
18
fig05_11.cpp(1 of 2)
1 // Fig. 5.11: fig05_11.cpp
2 // Printing a string one character at a time using
3 // a non-constant pointer to constant data.
4 #include
5
6 using std::cout;
7 using std::endl;
8
9 voidprintCharacters( const char * );10
11 intmain()
12 {
13 charphrase[] = "print characters of a string";
1415 cout
7/30/2019 Chapter05 Pointer String
19/77
2004 Trn Minh Chu.FOTECH. VNU.
19
fig05_11.cpp(2 of 2)
fig05_11.cpp
output (1 of 1)
23 // sPtr cannot modify the character to which it points,
24 // i.e., sPtr is a "read-only" pointer
25 voidprintCharacters( const char *sPtr )
26 {
27 for ( ; *sPtr != '\0'; sPtr++ ) // no initialization
28 cout
7/30/2019 Chapter05 Pointer String
20/77
7/30/2019 Chapter05 Pointer String
21/77
2004 Trn Minh Chu. FOTECH. VNU
21
Chng 5.
5.5 S dng const vi con tr
constpointers - hng con tr Lun trn vng nhcnh
l mc nh cho tn mng Phi c khi to khi khai bo
7/30/2019 Chapter05 Pointer String
22/77
2004 Trn Minh Chu.FOTECH. VNU.
22
fig05_13.cpp(1 of 1)
fig05_13.cpp
output (1 of 1)
1 // Fig. 5.13: fig05_13.cpp
2 // Attempting to modify a constant pointer to
3 // non-constant data.
4
5 intmain()
6 {
7 int x, y;
8
9 // ptr is a constant pointer to an integer that can10 // be modified through ptr, but ptr always points to the
11 // same memory location.
12 int * constptr = &x;
13
14*ptr = 7; // allowed: *ptr is not const
15 ptr = &y; // error: ptr is const; cannot assign new address
16
17 return 0; // indicates successful termination
18
19 } // end main
d:\cpphtp4_examples\ch05\Fig05_13.cpp(15) : error C2166:l-value specifies const object
ptr l hng con tr tr ti s nguyn.
C th thay i x (trbiptr) v x khng phi l hng
Khng th choptr trna ch mi vptr l hng
Dng 15 sinh ra li bin dch
v thay i a ch mi choconstant pointer.
7/30/2019 Chapter05 Pointer String
23/77
2004 Trn Minh Chu.FOTECH. VNU.
23
fig05_14.cpp(1 of 1)
1 // Fig. 5.14: fig05_14.cpp
2 // Attempting to modify a constant pointer to constant data.
3 #include
4
5 using std::cout;
6 using std::endl;
7
8 intmain()
9 {10 int x = 5, y;
11
12 // ptr is a constant pointer to a constant integer.
13 // ptr always points to the same location; the integer
14// at that location cannot be modified.
15 const int *constptr = &x;
16
17 cout
7/30/2019 Chapter05 Pointer String
24/77
7/30/2019 Chapter05 Pointer String
25/77
7/30/2019 Chapter05 Pointer String
26/77
2004 Trn Minh Chu.FOTECH. VNU.
26
fig05_15.cpp(2 of 3)
26 bubbleSort( a, arraySize ); // sort the array
27
28 cout
7/30/2019 Chapter05 Pointer String
27/77
2004 Trn Minh Chu.FOTECH. VNU.
27
fig05_15.cpp(3 of 3)
fig05_15.cpp
output (1 of 1)
51
52 } // end function bubbleSort
53
54 // swap values at memory locations to which
55 // element1Ptr and element2Ptr point
56 voidswap( int * const element1Ptr, int * const element2Ptr )
57 {
58 int hold = *element1Ptr;
59 *element1Ptr = *element2Ptr;60 *element2Ptr = hold;
61
62 } // end function swap
Data items in original order
2 6 4 8 10 12 89 68 45 37
Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Truyn tham chiu, cho phphm tro gi tr ti vng nh.
7/30/2019 Chapter05 Pointer String
28/77
2004 Trn Minh Chu. FOTECH. VNU
28
Chng 5.
5.6 Sp xp ni bt
s dng truyn tham chiu sizeof
Ton t tr v kch thc byte ca ton hng
Vi mng, sizeof tr v gi tr( kch thc 1 phn t ) * ( s phn t )
Nu sizeof( int ) = 4, thint myArray[10];
cout
7/30/2019 Chapter05 Pointer String
29/77
2004 Trn Minh Chu.FOTECH. VNU.
29
fig05_16.cpp(1 of 2)
1 // Fig. 5.16: fig05_16.cpp
2 // Sizeof operator when used on an array name
3 // returns the number of bytes in the array.
4 #include
5
6 using std::cout;
7 using std::endl;
8
9 size_t getSize( double * ); // prototype10
11 intmain()
12 {
13 double array[ 20 ];
14
15 cout
7/30/2019 Chapter05 Pointer String
30/77
31
7/30/2019 Chapter05 Pointer String
31/77
2004 Trn Minh Chu. FOTECH. VNU
31
Chng 5.
5.7 Cc php ton i vi con tr
Cc php ton con tr Tng/gim con tr (++ hoc --)
Cng/tr 1 s nguyn vi 1 con tr ( + hoc += , - hoc -=) Con tr c th tr ln nhau
Cng tr vi con tr l v ngha tr khi dng cho con tr mng
V d: Mng 5 phn tint trn my dng kiu int 4 byte vPtr trn phn t th nht v[ 0 ], ti a ch 3000
vPtr = 3000
vPtr += 2; tr vPtr ti 3008vPtr tr ti v[ 2 ]
bin con tr vPtr
v[0] v[1] v[2] v[4]v[3]
3000 3004 3008 3012 3016
vng nh
32
7/30/2019 Chapter05 Pointer String
32/77
2004 Trn Minh Chu. FOTECH. VNU
32
Chng 5.
5.7 Cc php ton i vi con tr
Tr con tr (Subtracting pointers) Tr v s phn t gia 2 a ch
vPtr2 = v[ 2 ];vPtr = v[ 0 ];vPtr2 - vPtr == 2
Gn con tr (Pointer assignment)
Mt con tr c thc gn cho con tr khc nu c haicng kiu Nu khng cng kiu th phi i kiu (cast) Ngoi l: con tr ti void(kiu void *)
con tr tng qut, i din cho kiu bt k khng cn i kiu chuyn sang con tr sang dng voidpointer
Khng th (dng *) ly d liu ca con tr kiu void
33
7/30/2019 Chapter05 Pointer String
33/77
2004 Trn Minh Chu. FOTECH. VNU
33
Chng 5.
5.7 Cc php ton i vi con tr
So snh con tr (Pointer comparison) S dng cc ton t quan h so snh a ch cha trong
con tr V d: c hai con tr trn hai phn t ca mt mng, chra con tr trn phn tc nh s th t cao
So snh l v ngha tr khi cc con tr trn cc phn t
ca cng mt mng Thng dng xc nh khi con tr c gi tr bng 0 (null)
(khng trn u c)
34
7/30/2019 Chapter05 Pointer String
34/77
2004 Trn Minh Chu. FOTECH. VNU
34
Chng 5.
5.8 Quan h gia Con tr v Mng
Mng v con tr c quan h cht ch Tn mng cng nh hng con tr (constant pointer)
C th dng ch si vi cc con tr Dng con tr truy nhp cc phn t mng
Phn tb[ n ] c th truy nhp bi *( bPtr + n )
k hiu pointer/offset a ch &b[ 3 ] tng ngbPtr + 3
Tn mng c th coi nh con tr
b[ 3 ] tng ng *( b + 3 )
Con tr c th vit vi cp ngoc vung (k hiupointer/subscript)bPtr[ 3 ] tng ngb[ 3 ]
35
7/30/2019 Chapter05 Pointer String
35/77
2004 Trn Minh Chu.FOTECH. VNU.
35
fig05_20.cpp(1 of 2)
1 // Fig. 5.20: fig05_20.cpp
2 // Using subscripting and pointer notations with arrays.
3
4 #include
5
6 using std::cout;
7 using std::endl;
8
9 intmain()10 {
11 intb[] = { 10, 20, 30, 40 };
12 int *bPtr = b; // set bPtr to point to array b
13
14 // output array b using array subscript notation
15 cout
7/30/2019 Chapter05 Pointer String
36/77
37b i d i h
7/30/2019 Chapter05 Pointer String
37/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_20.cppoutput (1 of 1)
Array b printed with:
Array subscript notation
b[0] = 10b[1] = 20
b[2] = 30
b[3] = 40
Pointer/offset notation where the pointer is the array name*(b + 0) = 10
*(b + 1) = 20
*(b + 2) = 30
*(b + 3) = 40
Pointer subscript notation
bPtr[0] = 10
bPtr[1] = 20
bPtr[2] = 30
bPtr[3] = 40
Pointer/offset notation
*(bPtr + 0) = 10
*(bPtr + 1) = 20
*(bPtr + 2) = 30
*(bPtr + 3) = 40
7/30/2019 Chapter05 Pointer String
38/77
3926
7/30/2019 Chapter05 Pointer String
39/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_21.cpp(2 of 2)
fig05_21.cpp
output (1 of 1)
26
27 } // end main
28
29 // copy s2 to s1 using array notation30 voidcopy1( char *s1, const char *s2 )
31 {
32 for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ )
33 ; // do nothing in body
3435 } // end function copy1
36
37 // copy s2 to s1 using pointer notation
38 voidcopy2( char *s1, const char *s2 )
39 {
40 for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ )
41 ; // do nothing in body
42
43 } // end function copy2
string1 = Hello
string3 = Good Bye
S dng ch s mng copy
xu ti s2 vo mng k t s1.
S dng k hiu con tr copy xuti s2 vo mng k t s1.
Tng c hai con tr trnphn t tip theo trong mng
tng ng.
40
7/30/2019 Chapter05 Pointer String
40/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.9 Mng con tr
Mng cha con tr Thng dng lu mng ca xu
char *suit[ 4 ] = {"Hearts", "Diamonds","Clubs", "Spades" };
Mi phn t ca suit trn char * (1 xu)
Mng khng cha xu, ch trn xu
Mng suit c kch thc cnh, nhng xu th khng
suit[3]
suit[2]
suit[1]
suit[0] H e a r t s \0
D i a m o n d s \0
C l u b s \0
S p a d e s \0
41
5 10 V d
7/30/2019 Chapter05 Pointer String
41/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.10 V d:Gi lp tro bi v chia bi T-l-kh
Chng trnh tro bi (Card shuffling program) Dng mt mng gm cc con tr trn xu lu tr tn
cc cht (suit), i.e. c(hearts), r (diamonds), pch (spades), tp (clubs) S dng mt mng hai chiu (hng: cht, ct: gi tr)
Ghi cc s t 1-52 vo mng lm th t chia cc con bi
deck[2][12] biu din K-tp
Hearts
Diamonds
Clubs
Spades
0
1
2
3
Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King0 1 2 3 4 5 6 7 8 9 10 11 12
Clubs King
7/30/2019 Chapter05 Pointer String
42/77
43
1 // Fig. 5.24: fig05 24.cpp
7/30/2019 Chapter05 Pointer String
43/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_24.cpp(1 of 4)
// g g _ pp
2 // Card shuffling dealing program.
3 #include
45 using std::cout;
6 using std::left;
7 using std::right;
8
9 #include10
11 using std::setw;
12
13 #include // prototypes for rand and srand
14 #include // prototype for time
15
16 // prototypes
17 voidshuffle( int [][ 13 ] );
18 voiddeal( const int [][ 13 ], const char *[], const char *[] );
19
20 intmain()21 {
22 // initialize suit array
23 const char *suit[ 4 ] =
24 { "Hearts", "Diamonds", "Clubs", "Spades" };
25
mng suit cha cc con trtrn cc mng char.
4426 // initialize face array
7/30/2019 Chapter05 Pointer String
44/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_24.cpp(2 of 4)
y
27 const char *face[ 13 ] =
28 { "Ace", "Deuce", "Three", "Four",
29 "Five", "Six", "Seven", "Eight",30 "Nine", "Ten", "Jack", "Queen", "King" };
31
32 // initialize deck array
33 int deck[ 4 ][ 13 ] = { 0 };
3435 srand( time( 0 ) ); // seed random number generator
36
37 shuffle( deck );
38 deal( deck, face, suit );
39
40 return 0; // indicates successful termination
41
42 } // end main
43
mng face cha cc con trtrn cc mng char.
7/30/2019 Chapter05 Pointer String
45/77
4666 // deal cards in deck
7/30/2019 Chapter05 Pointer String
46/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_24.cpp(4 of 4)
67 voiddeal( const intwDeck[][ 13 ], const char *wFace[],
68 const char *wSuit[] )
69 {70 // for each of the 52 cards
71 for ( int card = 1; card
7/30/2019 Chapter05 Pointer String
47/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_24.cppoutput (1 of 1)
Five of Spades Eight of Clubs
Queen of Diamonds Three of Hearts
Jack of Spades Five of DiamondsJack of Diamonds Three of Diamonds
Three of Clubs Six of Clubs
Ten of Clubs Nine of Diamonds
Ace of Hearts Queen of Hearts
Seven of Spades Deuce of SpadesSix of Hearts Deuce of Clubs
Ace of Clubs Deuce of Diamonds
Nine of Hearts Seven of Diamonds
Six of Spades Eight of Diamonds
Ten of Spades King of Hearts
Four of Clubs Ace of Spades
Ten of Hearts Four of Spades
Eight of Hearts Eight of Spades
Jack of Hearts Ten of Diamonds
Four of Diamonds King of Diamonds
Seven of Hearts King of SpadesQueen of Spades Four of Hearts
Nine of Clubs Six of Diamonds
Deuce of Hearts Jack of Clubs
King of Clubs Three of Spades
Queen of Clubs Five of ClubsFive of Hearts Ace of Diamonds
48
7/30/2019 Chapter05 Pointer String
48/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.11 Con tr ti hm (Function Pointer)
Con tr ti hm cha a ch ca hm
Tn mng c gi tr l a ch ca phn tu tin ca mng
Tng t, tn hm c gi tr l a ch bt u ca on mnh ngha hm
Cc con tr ti hm c th
c truyn vo trong hm c tr v t hm
c lu trong mng
c gn cho cc con tr hm khc
49
7/30/2019 Chapter05 Pointer String
49/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.11 Con tr ti hm
Gi hm bng con tr ti hm gi s compare c khai bo l con tr ti hm c kiu
tham s v kiu tr v nh sau:bool ( *compare ) ( int, int )
gi hm bng mt trong hai cch ( *compare ) ( int1, int2 )
thm nhp con tr chy hm c con tr tr tiHOC compare( int1, int2 )
d nhm ln
ngi dng c th tng compare l tn ca hmthc trong chng trnh
501 // Fig. 5.25: fig05_25.cpp
2 // l i i i f i i
7/30/2019 Chapter05 Pointer String
50/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_25.cpp(1 of 5)
2 // Multipurpose sorting program using function pointers.
3 #include
45 using std::cout;
6 using std::cin;
7 using std::endl;
8
9#include
10
11 using std::setw;
12
13 // prototypes
14 voidbubble( int [], const int,bool (*)( int, int ) );
15 voidswap( int * const, int * const );
16 bool ascending( int, int );
17 bool descending( int, int );
18
19 intmain()
20 {21 const int arraySize = 10;
22 int order;
23 int counter;
24 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
25
Tham s thba l con tr timt hm nhn 2 tham s intv tr v kt qu kiubool.
5126 cout
7/30/2019 Chapter05 Pointer String
51/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_25.cpp(2 of 5)
27 > order;
29 cout
7/30/2019 Chapter05 Pointer String
52/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_25.cpp(3 of 5)
( ; y ; )
51 cout
7/30/2019 Chapter05 Pointer String
53/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_25.cpp(4 of 5)
76 // swap values at memory locations to which
77 // element1Ptr and element2Ptr point
78 voidswap( int * const element1Ptr, int * const element2Ptr )79 {
80 int hold = *element1Ptr;
81 *element1Ptr = *element2Ptr;
82 *element2Ptr = hold;
83
84 } // end function swap
85
86 // determine whether elements are out of order
87 // for an ascending order sort
88 bool ascending( int a, intb )
89 {
90 returnb < a;// swap if b is less than a
91
92 } // end function ascending
93
94 // determine whether elements are out of order95 // for a descending order sort
96 bool descending( int a, intb )
97 {
98 returnb > a; // swap if b is greater than a
99
100 } // end function descending
Enter 1 to sort in ascending order,Enter 2 to sort in descending order: 1
Data items in original order2 6 4 8 10 12 89 68 45 37
Data items in ascending order2 4 6 8 10 12 37 45 68 89
Enter 1 to sort in ascending order,
Enter 2 to sort in descending order: 2
Data items in original order
2 6 4 8 10 12 89 68 45 37
Data items in descending order
89 68 45 37 12 10 8 6 4 2
54
5 11 Con tr ti hm
7/30/2019 Chapter05 Pointer String
54/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.11 Con tr ti hm
Mng gm cc con tr hm Thng dng cho cc h thng iu khin bng thc n
(menu-driven system)
Cc con trn tng hm c lu trong mng con tr hm cc hm u phi c kiu d liu tr v ging nhau, v kiu d
liu ca tham s nh nhau
nh x(la chn thc n ch s trong mng con tr ti hm)
55
1 // Fig. 5.26: fig05_26.cpp
2 // Demonstrating an array of pointers to functions.
7/30/2019 Chapter05 Pointer String
55/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_26.cpp(1 of 3)
2 // Demonstrating an array of pointers to functions.
3 #include
45 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 // function prototypes
10 voidfunction1( int );
11 voidfunction2( int );
12 voidfunction3( int );
13
14 intmain()
15 {
16 // initialize array of 3 pointers to functions that each
17 // take an int argument and return void
18 void(*f[ 3 ])( int ) = { function1, function2, function3 };
19
20 int choice;21
22 cout > choice;
24
Mng c khi to vi tn ca ba hm,tn ca hm chnh l con tr.
5625 // process user's choice
26 while ( choice >= 0 && choice < 3 ) {
7/30/2019 Chapter05 Pointer String
56/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_26.cpp(2 of 3)
( ) {
27
28 // invoke function at location choice in array f29 // and pass choice as an argument
30 (*f[ choice ])( choice );
31
32 cout > choice;
34 }
35
36 cout
7/30/2019 Chapter05 Pointer String
57/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_26.cpp(3 of 3)
fig05_26.cpp
output (1 of 1)
{
51 cout
7/30/2019 Chapter05 Pointer String
58/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.12.1 Tng kt v k t v xu k t
Hng k t - Character constant Gi tr nguyn biu din di dng mt k t vit trong 2 du nhy
'z' l gi tr nguyn ca k tz M 122 trong bng m ASCII
Xu k t - String Chui cc k tc coi nh l mt single unit
C th bao gm ch ci, ch s, k tc bit +, -, * ... Hng xu k t - String literal (string constants)
Vit trong cp nhy kp, v d: "I like C++"
Mng ca cc k t, kt thc vi k t rng (null character) '\0'
Xu l mt hng con tr (constant pointer) Trn k tu tin ca xu
Ging nh vi mng
59
5 12 1 Tng kt v k t v xu k t
7/30/2019 Chapter05 Pointer String
59/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.12.1 Tng kt v k t v xu k t
Gn gi tr cho xu - String assignment Mng ca k t
char color[] = "blue";
To mng color 5 phn t kiu char
phn t cui cng l '\0'
Bin kiu char *
char *colorPtr = "blue"; To con tr colorPtr tr n chb trong xu "blue"
"blue" u trong b nh
Mt cch khc cho mng k t char color[] = { 'b', 'l', 'u', 'e', '\0' };
60
5 12 1 Tng kt v k t v xu k t
7/30/2019 Chapter05 Pointer String
60/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.12.1 Tng kt v k t v xu k t
c xu c d liu cho mng k tword[ 20 ]
cin >> word
c cc k t cho n khi gp k t trng hoc EOF
Xu c th vt qu kch thc mngcin >> setw( 20 ) >> word;
c 19 k t ( li ch cho '\0')
cin.getline
c 1 dng vn bn cin.getline( array, size, delimiter );
Lu input vo mng arrayn khi xy ra mt trong hai trng hp Kch thc d liu t n size 1
K tdelimiterc nhp vo V d
char sentence[ 80 ];
cin.getline( sentence, 80, '\n' );
61
5.12.2 Cc hm x l xu k t
7/30/2019 Chapter05 Pointer String
61/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.12.2 Cc hm x l xu k t
Th vin x l xucung cp cc hm thao tc vi d liu kiu xu
so snh xu
tm kim trn xu cc k t hoc xu khc chia xu thnh cc t t (tokenize strings)
62
5.12.2 Cc hm x l xu k t
7/30/2019 Chapter05 Pointer String
62/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5.12.2 Cc hm x l xu k t
So snh xu s1 v xu s2. Hm tr v gi tr0, nh hn 0, hoc ln hn 0 nu s1bng, nhhn hoc ln hn s2.
int strcmp( const char *s1,const char *s2 );
Thm xu nhiu nht l n k t ca s2 vosau xu s1. K tu tin ca s2 ghi lnk t null ca s1. Tr v gi tr ca s1.
char *strncat( char *s1, constchar *s2, size_t n );
Thm xu s2 vo sau xu s1. K tu tinca s2 ghi ln k t null ca s1. Tr v gitr ca s1.
char *strcat( char *s1, constchar *s2 );
Copy nhiu nht n k t ca xu s2 vo xus1. Tr v gi tr ca s1.
char *strncpy( char *s1, constchar *s2, size_t n );
Copy xu s2 vo xu s1. Tr v gi tr cas1.
char *strcpy( char *s1, constchar *s2 );
63
5.12.2 Cc hm x l xu k t
7/30/2019 Chapter05 Pointer String
63/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
5 Cc u t
Xc nh di ca xu s. Tr v s k tca xu (khng tnh k t null).
size_t strlen( const char *s );
Mt chui li gi n strtok chia xus1 thnh cc tokenst t, chng hncc t trong mt dng vn bnphn tchnhau bi cc k t cha trong xu s2.Li gi u tin ly s1 lm tham s thnht, cc li gi tip sau (viNULL l thams th nht) tip tc ly cc t t t chnhxu .Mi li gi tr v mt con tr ti t t vanhn c. Nu khng cn t t no, hm
s tr v gi trNULL.
char *strtok( char *s1, const char*s2 );
So snh n k t xu s1 v xu s2. Hmtr v gi tr 0, nh hn 0 hoc ln hn 0nu s1bng, nh hn hoc ln hn s2.
int strncmp( const char *s1, constchar *s2, size_t n );
64
5.12.2 Cc hm x l xu k t
7/30/2019 Chapter05 Pointer String
64/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
Copy xu char *strcpy( char *s1, const char *s2 )
Copy tham s th hai vo tham s th nht
Tham s th nht phi c kch thc ln cha xuv k t null
char *strncpy( char *s1, const char *s2,
size_t n ) Xc nh r s k tc copy t xu vo mng
Khng nht thit copy k t null
651 // Fig. 5.28: fig05_28.cpp
2 // Using strcpy and strncpy.
3 #include
7/30/2019 Chapter05 Pointer String
65/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_28.cpp
(1 of 2)
3 #include
45 using std::cout;
6 using std::endl;
7
8 #include // prototypes for strcpy and strncpy
9
10 intmain()
11 {
12 char x[] = "Happy Birthday to You";
13 char y[ 25 ];
14 char z[ 15 ];
1516 strcpy( y, x ); // copy contents of x into y
17
18 cout
7/30/2019 Chapter05 Pointer String
66/77
2004 Trn Minh Chu.FOTECH. VNU.
fig05_28.cpp
(2 of 2)
fig05_28.cpp
output (1 of 1)
29 } // end main
The string in array x is: Happy Birthday to You
The string in array y is: Happy Birthday to You
The string in array z is: Happy Birthday
Xu gc.
Copy xu bng strcpy.
Copy 14 k tu tinbng strncpy.
7/30/2019 Chapter05 Pointer String
67/77
68
1 // Fig. 5.29: fig05_29.cpp
2 // Using strcat and strncat.
3 #include
7/30/2019 Chapter05 Pointer String
68/77
2004 Trn Minh Chu.
FOTECH. VNU.
fig05_29.cpp
(1 of 2)
3 #include
45 using std::cout;
6 using std::endl;
7
8 #include // prototypes for strcat and strncat
9
10 intmain()
11 {
12 char s1[ 20 ] = "Happy ";
13 char s2[] = "New Year ";
14 char s3[ 40 ] = "";
1516 cout
7/30/2019 Chapter05 Pointer String
69/77
2004 Trn Minh Chu.
FOTECH. VNU.
fig05_29.cpp
(2 of 2)
fig05_29.cpp
output (1 of 1)
28
29 strcat( s3, s1 ); // concatenate s1 to s330 cout
7/30/2019 Chapter05 Pointer String
70/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
So snh xu - Comparing strings Cc k tc biu din bng m dng s (numeric code)
cc m c dng so snh cc xu k t Cc b m k t (Character codes / character sets)
ASCII American Standard Code for Information Interchage EBCDIC Extended Binary Coded Decimal Interchange Code
Cc hm so snh xu int strcmp( const char *s1, const char *s2 )
So snh tng k t mt, theo th t tin Tr v 0 nu xu bng nhau Gi tr m nu xu th nht nh hn xu th hai Gi tr dng nu xu th nht ln hn xu th hai
int strncmp( const char *s1,const char *s2, size_t n )
So snh n k tu tin Dng so snh nu gp k t null ca 1 trong 2 tham s
711 // Fig. 5.30: fig05_30.cpp
2 // Using strcmp and strncmp.
3 #include
7/30/2019 Chapter05 Pointer String
71/77
2004 Trn Minh Chu.
FOTECH. VNU.
fig05_30.cpp
(1 of 2)45 using std::cout;
6 using std::endl;
7
8 #include
9
10 using std::setw;
11
12 #include // prototypes for strcmp and strncmp
13
14 intmain()
15 {16 char *s1 = "Happy New Year";
17 char *s2 = "Happy New Year";
18 char *s3 = "Happy Holidays";
19
20cout
7/30/2019 Chapter05 Pointer String
72/77
2004 Trn Minh Chu.
FOTECH. VNU.
fig05_30.cpp
(2 of 2)
fig05_30.cpp
output (1 of 1)
27 cout
7/30/2019 Chapter05 Pointer String
73/77
74
fi 05 31
1 // Fig. 5.31: fig05_31.cpp
2 // Using strtok.
3 #include
7/30/2019 Chapter05 Pointer String
74/77
2004 Trn Minh Chu.
FOTECH. VNU.
fig05_31.cpp
(1 of 2)45 using std::cout;
6 using std::endl;
7
8 #include // prototype for strtok
9
10 intmain()
11 {
12 char sentence[] = "This is a sentence with 7 tokens";
13 char *tokenPtr;
14
15 cout
7/30/2019 Chapter05 Pointer String
75/77
2004 Trn Minh Chu.
FOTECH. VNU.
The string to be tokenized is:
This is a sentence with 7 tokens
The tokens are:
This
is
asentence
with
7
tokens
After strtok, sentence = This
fig05_31.cpp
(2 of 2)24
tokenPtr = strtok(NULL, " " ); // get next token25
26 } // end while
27
28 cout
7/30/2019 Chapter05 Pointer String
76/77
2004 Trn Minh Chu. FOTECH. VNU Chng 5.
Xc nh di xu size_t strlen( const char *s )
Tr v s k t ca xu
Khng tnh n k t null
77
fig05 32 cpp
1 // Fig. 5.32: fig05_32.cpp
2 // Using strlen.
3 #include
4
7/30/2019 Chapter05 Pointer String
77/77
2004 Trn Minh Chu.
FOTECH. VNU.
fig05_32.cpp
(1 of 1)4
5 using std::cout;
6 using std::endl;
7
8 #include // prototype for strlen
9
10 intmain()
11 {
12 char *string1 = "abcdefghijklmnopqrstuvwxyz";
13 char *string2 = "four";
14 char *string3 = "Boston";
1516 cout