Πανεπιστήµιο Πατρών ΟΝΤΟΚΕΝΤΡΙΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΙΙ (C++) είκτες και Συµβολοσειρές (Pointers. & Strings)

Σχετικά έγγραφα
ΗΥ-150. Προγραμματισμός

Δείκτες (Pointers) (1/2) ΗΥ150 Προγραμματισμός

Οντοκεντρικός Προγραμματισμός

Προαπαιτούμενες Ασκήσεις 5 ου Εργαστηρίου. Dose stoixeio (integer) : 25 Found stoixeio in position 7 Dose stoixeio (integer) :94 Value not found

Συµβολοσειρές - Strings

Παράδειγµα χρήσης πίνακα

Διάλεξη 12η: Δείκτες, μέρος 2

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

HY150a Φροντιστήριο 3 24/11/2017

Γλώσσα Προγραμματισμού C++ Εισαγωγή - Μια πρώτη ματιά

Στόχοι και αντικείμενο ενότητας. Πέρασμα Πίνακα σε Συνάρτηση (συν.) Πέρασμα Πίνακα σε Συνάρτηση. #8.. Ειδικά Θέματα Αλγορίθμων

Συναρτήσεις στη C++ Οι µεταβλητές χαρακτηρίζονται από διάφορες ιδιότητες. Για ποιο διάστηµα η µεταβλητή υπάρχει στη µνήµη

Διάλεξη 5: Δείκτες και Συναρτήσεις

17TimeThis.h function returns reference pointer to same object { return *this; }

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

Οντοκεντρικός Προγραμματισμός

Εντολές εισόδου - εξόδου. Εισαγωγή στη C++

Προγραμματισμός Ι. Δείκτες. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

ΕΡΓΑΣΤΗΡΙΟ 1 - ΣΗΜΕΙΩΣΕΙΣ

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

Οντοκεντρικός Προγραμματισμός

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Instruction Execution Times

Συναρτήσεις και Πίνακες

ΗΥ-150. Πίνακες (Arrays)

Διάλεξη 11η: Δείκτες, μέρος 1

Διάλεξη 9η: Πίνακες (arrays)

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 11/3/2006

ΕΠΛ232 Προγραμματιστικές Τεχνικές και Εργαλεία Δείκτες και Συναρτήσεις (Κεφάλαιο 11, KNK-2ED)

Προγραμματισμός Ι. Δυναμική Διαχείριση Μνήμης. Δημήτρης Μιχαήλ. Ακ. Έτος Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

Αναφορές, είκτες και Αλφαριθμητικά

Προγραμματισμός Η/Υ. Ενότητα 8: Ειδικά Θέματα Αλγορίθμων

ΕΙΣΑΓΩΓΗ ΣΤΟN ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ ΚΑΙ ΠΛΗΡΟΦΟΡΙΚΗΣ

Δήλωση ακέραιων μεταβλητών. ροής με την στάνταρ είσοδο

Αντικειμενοστραφείς Γλώσσες Προγραμματισμού C++ / ROOT

Προγραμματισμός Επιπλέον στοιχεία της C++

Εισαγωγή στον Προγραμματισμό

Τμήμα Πληροφορικής & Επικοινωνιών Δρ. Θεόδωρος Γ. Λάντζος

(C) 2010 Pearson Education, Inc. All rights reserved.

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Οντοκεντρικός Προγραμματισμός

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Κλάσεις και Αντικείμενα Αναφορές

Προγραμματισμός Η/Υ (ΤΛ2007 )

Εισαγωγή στον Προγραμματισμό

ΠΛΗ111. Ανοιξη Μάθηµα 1 ο Ανασκόπηση της Γλώσσας Προγραµµατισµού C. Τµήµα Ηλεκτρονικών Μηχανικών και Μηχανικών Υπολογιστών Πολυτεχνείο Κρήτης

A ΜΕΡΟΣ. 1 program Puppy_Dog; 2 3 begin 4 end. 5 6 { Result of execution 7 8 (There is no output from this program ) 9 10 }

Η γλώσσα C. Δείκτες (pointers)

Προηγµένα Θέµατα Τεχνολογιών Υλοποίησης Αλγορίθµων

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

Αντικειμενοστραφής Προγραμματισμός

Στόχοι και αντικείμενο ενότητας. Εκφράσεις. Η έννοια του τελεστή. #2.. Εισαγωγή στη C (Μέρος Δεύτερο) Η έννοια του Τελεστή

Η Γλώσσα Προγραµµατισµού C++ (The C++ Programming Language)

Η Γλώσσα Προγραµµατισµού C++ (The C++ Programming Language) Ιστοσελίδα του µαθήµατος. Περιεχόµενα. ηµήτριος Κατσαρός, Ph.D. Πίνακες.

ΕισαγωγήστουςΗ/Υ. PHP Hypertext Preprocessor

ΒΑΣΙΚΟΙ ΤΥΠΟΙ ΚΑΙ ΠΙΝΑΚΕΣ

Προγραμματισμός Ι. Πίνακες, Δείκτες, Αναφορές και Δυναμική Μνήμη. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

The Simply Typed Lambda Calculus

Προγραμματισμός Ι. Εγγραφές. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

2 Composition. Invertible Mappings

Στοιχειώδης προγραμματισμός σε C++

Μορφοποίηση υπό όρους : Μορφή > Μορφοποίηση υπό όρους/γραμμές δεδομένων/μορφοποίηση μόο των κελιών που περιέχουν/

Μεταφραστής (Compiler)

#define, 70, 575 #elif, 580 #else, 580 #endif, 580 #error, 584 #if, 580 #ifdef, 583 #ifndef, 580, 583 #include, 70, 227, 574 #undef, 579

Δομημένος Προγραμματισμός (ΤΛ1006)

Προγραμματισμός Ι. Κλάσεις και Αντικείμενα. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

ΑΡΧΕΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ

υναµική διαχείριση µνήµης στη C++ Στην ενότητα αυτή θα µελετηθούν τα εξής επιµέρους θέµατα: ΕΠΛ 132 Αρχές Προγραµµατισµού ΙΙ 2 είκτες

Μετατροπή χαρακτήρων ASCII σε ακέραιο αριθµό (atoi) & Άνοιγµα αρχείου µέσα από τo QtSPIM, διάβασµα, και αποθήκευση του περιεχοµένου του στη µνήµη

ΗΥ-150. Προγραµµατισµός. Πίνακες (Arrays)

ΑΣΚΗΣΗ 6: ΔΕΙΚΤΕΣ. Σκοπός της Άσκησης. 1. Εισαγωγικά στοιχεία για τους Δείκτες

Οντοκεντρικός Προγραμματισμός

ΗΥ-150 Προγραμμαηιζμός Δείκηες (Pointers) (1/2)

Π. Σταθοπούλου ή Οµάδα Α (Φοιτητές µε µονό αριθµό Μητρώου ) ιδασκαλία : Παρασκευή 11πµ-13µµ ΗΛ7

Χpήσιµες Βιβλιοθήκες της γλώσσας C

ΗΥ-150. Προγραμματισμός

Αρχεία & Ρεύματα ΑΡΧΕΙΑ & ΡΕΥΜΑΤΑ. Γεώργιος Παπαϊωάννου ( ) gepap@aueb.gr

Η εντολή if-else. Η απλή μορφή της εντολής if είναι η ακόλουθη: if (συνθήκη) { Η γενική μορφή της εντολής ifelse. εντολή_1; εντολή_2;..

Κεφάλαιο 8.7. Πίνακες & Συναρτήσεις ( ιάλεξη 17) ιδάσκων: ηµήτρης Ζεϊναλιπούρ

Κλήση Συναρτήσεων ΚΛΗΣΗ ΣΥΝΑΡΤΗΣΕΩΝ. Γεώργιος Παπαϊωάννου ( )

Δομημένος Προγραμματισμός (ΤΛ1006)

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ Ανώτατο Εκπαιδευτικό Ίδρυμα Πειραιά Τεχνολογικού Τομέα. Προγραμματισμός Η/Υ

Διδάσκων: Παναγιώτης Ανδρέου

Δομημένος Προγραμματισμός. Τμήμα Επιχειρηματικού Σχεδιασμού και Πληροφοριακών Συστημάτων

Φροντιςτήριο. Linked-List

Διαδικασίες ΙI. ΗΥ 134 Εισαγωγή στην Οργάνωση και στον Σχεδιασμό Υπολογιστών Ι. Διάλεξη 5

Η γλώσσα C. Δείκτες (pointers)

Μεθόδων Επίλυσης Προβλημάτων

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Δείκτες σε συναρτήσεις. Προγραμματισμός II 1

Η Γλώσσα Προγραµµατισµού C++ (The C++ Programming Language)

Διάλεξη 8η: Αλφαριθμητικά (strings)

Οντοκεντρικός Προγραμματισμός

Διάλεξη 2: Επανάληψη Προγραμματισμού Συμβολοσειρές (strings) Διδάσκων: Παναγιώτης Ανδρέου

Εισαγωγή στον Προγραμματισμό

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Δομημένος Προγραμματισμός (ΤΛ1006)

Προγραμματισμός Υπολογιστών με C++

Αρχές Προγραμματισμού

Transcript:

Πανεπιστήµιο Πατρών 1 Τµήµα Μηχανικών Ηλεκτρονικών Υπολογιστών και Πληροφορικής ΟΝΤΟΚΕΝΤΡΙΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΙΙ (C++) είκτες και Συµβολοσειρές (Pointers & Strings)

Περιεχόµενα 2 Εισαγωγή ήλωση και αρχικοποίηση µεταβλητών τύπου δείκτη Τελεστές δεικτών τελεστής διεύθυνσης & τελεστής έµµεσης αναφοράς * Κλήση συναρτήσεων και πέρασµα ορισµάτων µε αναφορά Χρήση του const µε δείκτες Σταθεροί δείκτες Bubble Sort µε δείκτες Τελεστής sizeof Εκφράσεις και αριθµητική δεικτών Σχέση δεικτών και πινάκων Πίνακες δεικτών είκτες συναρτήσεων Συναρτήσεις διαχείρισης συµβολοσειρών Ασκήσεις

3 Εισαγωγή είκτες (Pointers) Ισχυρό χαρακτηριστικό, αλλά δύσκολη η διαχείρισή τους Υλοποιούνέναείδος pass-by-reference Στενή σχέση µε τους πίνακες και τα strings

ήλωση και αρχικοποίηση µεταβλητών τύπου δείκτη Μεταβλητές τύπου δείκτη Αποθηκεύουν διευθύνσεις µνήµης ως τιµές Μια κανονική µεταβλητή αποθηκεύει συγκεκριµένες τιµές (άµεσηαναφορά - direct reference) Ένας δείκτης αποθηκεύει τη διεύθυνση µιας µεταβλητής που περιέχειµιασυγκεκριµένητιµή (έµµεσηαναφορά - indirect reference) Έµµεση αναφορά (Indirection) Η αναφορά µιας τιµής µέσω ενός δείκτη ήλωση δείκτη countptr Το * στη δήλωση υποδηλώνει ότι η µεταβλητή είναι δείκτης int *myptr; Περισσότεροι δείκτες χρειάζονται και περισσότερα * int *myptr1, *myptr2; count count 7 7 4

ήλωση και αρχικοποίηση µεταβλητών τύπου δείκτη 5 Οι δείκτες µπορούν να χρησιµοποιηθούν µε οποιοδήποτε τύπο δεδοµένων. Αρχικοποίηση δείκτη Αρχικοποιείται στο 0 ή NULL 0 ή NULL δείχνουν στο κενό Η συµβολική σταθερά NULL ορίζεται στο αρχείο επικεφαλίδας <iostream>

6 Τελεστές δεικτών & (τελεστής διεύθυνσης) Επιστρέφει τη διεύθυνση µνήµης του τελεσταίου Παράδειγµα int y = 5; int *yptr; yptr = &y; // yptr gets address of y yptr points to y yptr y 5 yptr 500000 600000 y 600000 5 address of y is value of yptr

7 Τελεστές δεικτών * (τελεστής έµµεσης αναφοράς ή αποαναφοράς) Επιστρέφει το αντικείµενο που δείχνει ο δείκτης *yptr επιστρέφει το y (αφού yptr δείχνει στο y). Ένας αποαναφοροποιηµένος δείκτης είναι ένα lvalue *yptr = 9; // assigns 9 to y * και & είναι αντίστροφοι τελεστές (αλληλοαναιρούνται)

1 // Fig. 5.4: fig05_04.cpp 2 // Using the & and * operators. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 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 a 15 16 cout << "The address of a is " << &a 17 << "\nthe value of aptr is " << aptr; 18 19 cout << "\n\nthe value of a is " << a 20 << "\nthe value of *aptr is " << *aptr; 21 22 cout << "\n\nshowing that * and & are inverses of " 23 << "each other.\n&*aptr = " << &*aptr 24 << "\n*&aptr = " << *&aptr << endl; 25 fig05_04.cpp (1 of 2) * and & are inverses of each other 8

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 * and & are inverses; same result when both applied to aptr fig05_04.cpp (2 of 2) fig05_04.cpp output (1 of 1) 9

Κλήση συναρτήσεων και πέρασµα ορισµάτων µε αναφορά 10 3 τρόποι να περαστούν τα ορίσµατα σε µια συνάρτηση Pass-by-value Pass-by-reference with reference arguments Pass-by-reference with pointer arguments Με return µπορούµε να επιστρέψουµε µια µόνο τιµή από τη συνάρτηση Με τα ορίσµατα που περνούν µε αναφορά είναι δυνατή η αλλαγή των αρχικών τιµών των ορισµάτων είναι δυνατή η επιστροφή από µια συνάρτηση περισσότερων τιµών

Κλήση συναρτήσεων και πέρασµα ορισµάτων µε αναφορά 11 Pass-by-reference µε ορίσµατα δείκτες Περνάµε τη διεύθυνση του ορίσµατος χρησιµοποιώντας τον τελεστή διεύθυνσης & Οι πίνακες δεν χρειάζονται τον τελεστή & αφού το όνοµα του πίνακα αντιστοιχεί ήδη σε δείκτη Χρησιµοποιούµε τον τελεστή έµµεσης αναφοράς * για την προσπέλαση των τιµών των µεταβλητών εντός της συνάρτησης

1 // Fig. 5.6: fig05_06.cpp 2 // Cube a variable using pass-by-value. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int cubebyvalue( int ); // prototype 9 10 int main() 11 { 12 int number = 5; 13 14 cout << "The original value of number is " << number; 15 16 // pass number by value to cubebyvalue 17 number = cubebyvalue( number ); 18 Pass number by value; result returned by cubebyvalue 19 cout << "\nthe new value of number is " << number << endl; 20 21 return 0; // indicates successful termination 22 23 } // end main 24 fig05_06.cpp (1 of 2) 12

25 // calculate and return cube of integer argument 26 int cubebyvalue( int n ) 27 { 28 return n * n * n; // cube local variable cubebyvalue n and return receives result 29 parameter passed-by-value 30 } // end function cubebyvalue The original value of number is 5 The new value of number is 125 Cubes and returns local variable n fig05_06.cpp (2 of 2) fig05_06.cpp output (1 of 1) 13

1 // Fig. 5.7: fig05_07.cpp 2 // Cube a variable using pass-by-reference 3 // with a pointer argument. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 void cubebyreference( int * ); // prototype 10 11 int main() 12 { 13 int number = 5; Prototype indicates parameter is pointer to int 14 15 cout << "The original value of number is " << number; 16 Apply address operator & to pass address of number to cubebyreference 17 // pass address of number to cubebyreference 18 cubebyreference( &number ); 19 20 cout << "\nthe new value of number is " << number << endl; 21 22 return 0; // indicates successful termination 23 24 } // end main 25 fig05_07.cpp (1 of 2) cubebyreference modified variable number 14

26 // calculate cube of *nptr; modifies variable number in main 27 void cubebyreference( 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 receives address of int variable, i.e., pointer to an int Modify and access int variable using indirection operator * fig05_07.cpp (2 of 2) fig05_07.cpp output (1 of 1) 15

16 const qualifier Χρήση του const µε δείκτες εν επιτρέπει την αλλαγή της τιµής της µεταβλητής Βάζουµε το const όταν η συνάρτηση δεν χρειάζεται να αλλάξει την τιµή της µεταβλητής Αρχή του ελαχίστου δικαιώµατος Μια συνάρτηση δεν χρειάζεται να αποκτά παραπάνω δικαιώµατα από αυτά που χρειάζεται για την επίτευξη της εργασίας της 4 τρόποι για να περάσουµε ένα δείκτη σε µια συνάρτηση Μεταβλητός δείκτης σε µεταβλητά δεδοµένα Μέγιστο δικαίωµα προσπέλασης Μεταβλητός δείκτης σε σταθερά δεδοµένα Σταθερός δείκτης σε µεταβλητά δεδοµένα Σταθερός δείκτης σε σταθερά δεδοµένα Ελάχιστο δικαίωµα προσπέλασης

1 // Fig. 5.10: fig05_10.cpp 2 // Converting lowercase letters to uppercase letters 3 // using a non-constant pointer to non-constant data. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 #include <cctype> // prototypes for islower and toupper 10 11 void converttouppercase( char * ); 12 13 int main() 14 { 15 char phrase[] = "characters and $32.98"; 16 17 cout << "The phrase before conversion is: " << phrase; 18 converttouppercase( phrase ); 19 cout << "\nthe phrase after conversion is: " 20 << phrase << endl; 21 22 return 0; // indicates successful termination 23 24 } // end main 25 Parameter is nonconstant pointer to nonconstant data converttouppercase modifies variable phrase fig05_10.cpp (1 of 2) 17

26 // convert string to uppercase letters 27 void converttouppercase( char *sptr ) 28 { 29 while ( *sptr!= '\0' ) { // current character is not '\0' 30 31 if ( islower( *sptr ) ) // if character is lowercase, 32 *sptr = toupper( *sptr ); // convert to uppercase 33 Function islower returns 34 ++sptr; // move sptr to next character in string 35 36 } // end while 37 true if character is lowercase 38 } // end function converttouppercase Function toupper returns When corresponding operator uppercase ++ applied to pointer character that if original points to character array, The phrase before conversion is: characters and $32.98 The phrase after conversion is: memory lowercase; CHARACTERS address otherwise AND stored $32.98 in pointer toupper modified returns to original point to next (uppercase) element character of array. fig05_10.cpp Parameter sptr nonconstant (2 of 2) pointer to nonconstant data fig05_10.cpp output (1 of 1) 18

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 <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 void printcharacters( const char * ); 10 11 int main() 12 { 13 char phrase[] = "print characters of a string"; 14 15 cout << "The string is:\n"; 16 printcharacters( phrase ); 17 cout << endl; 18 19 return 0; // indicates successful termination 20 21 } // end main 22 Parameter is nonconstant pointer to constant data. Pass pointer phrase to function printcharacters. fig05_11.cpp (1 of 2) 19

23 // sptr cannot modify the character to which it points, 24 // i.e., sptr is a "read-only" pointer 25 void printcharacters( const char *sptr ) 26 { 27 for ( ; *sptr!= '\0'; sptr++ ) // no initialization 28 cout << *sptr; 29 30 } // end function printcharacters The string is: print characters of a string sptr is nonconstant pointer to constant data; cannot modify character to which sptr points. Increment sptr to point to next character. fig05_11.cpp (2 of 2) fig05_11.cpp output (1 of 1) 20

1 // Fig. 5.12: fig05_12.cpp 2 // Attempting to modify data through a 3 // non-constant pointer to constant data. 4 5 void f( const int * ); // prototype 6 7 int main() 8 { 9 int y; 10 11 f( &y ); // f attempts illegal modification 12 13 return 0; // indicates successful termination 14 15 } // end main 16 17 // xptr cannot modify the value of the variable 18 // to which it points 19 void f( const int *xptr ) 20 { 21 *xptr = 100; // error: cannot modify a const object 22 23 } // end function f Parameter is nonconstant pointer to constant data. Pass address of int variable y to attempt illegal modification. Attempt to modify const object pointed to by xptr. d:\cpphtp4_examples\ch05\fig05_12.cpp(21) : error C2166: l-value specifies const object Error produced when attempting to compile. fig05_12.cpp (1 of 1) fig05_12.cpp output (1 of 1) 21

22 Σταθεροί δείκτες const pointers Πάντα δείχνουν στην ίδια θέση µνήµης Η περίπτωση του ονόµατος ενός πίνακα Πρέπει να αρχικοποιείται όταν δηλώνεται

1 // Fig. 5.13: fig05_13.cpp 2 // Attempting to modify a constant pointer to 3 // non-constant data. 4 5 int main() 6 { 7 int x, y; 8 9 // ptr is a constant pointer to an integer that can 10 // be modified through ptr, but ptr always points to the 11 // same memory location. 12 int * const ptr = &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 is constant pointer to integer. Can modify x (pointed to by ptr) Cannot since modify x not ptr constant. to point to new address since ptr is constant. Line 15 generates compiler error by attempting to assign new address to constant pointer. fig05_13.cpp (1 of 1) fig05_13.cpp output (1 of 1) 23

1 // Fig. 5.14: fig05_14.cpp 2 // Attempting to modify a constant pointer to constant data. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int x = 5, y; 11 12 // ptr is a constant pointer to a constant ptr is constant integer. pointer to 13 // ptr always points to the same location; integer the constant. integer 14 // at that location cannot be modified. 15 const int *const ptr = &x; 16 Cannot modify x (pointed to 17 cout << *ptr << endl; Cannot by ptr) modify since *ptr to declared point 18 to constant. new address since ptr is 19 *ptr = 7; // error: *ptr constant. is const; cannot assign new value 20 ptr = &y; // error: ptr is const; cannot assign new address 21 22 return 0; // indicates successful termination 23 24 } // end main fig05_14.cpp (1 of 1) 24

d:\cpphtp4_examples\ch05\fig05_14.cpp(19) : error C2166: l-value specifies const object d:\cpphtp4_examples\ch05\fig05_14.cpp(20) : error C2166: l-value specifies const object Line 19 generates compiler fig05_14.cpp error by attempting to modify output (1 of 1) Line constant 20 generates object. compiler error by attempting to assign new address to constant pointer. 25

26 Bubble Sort µε δείκτες Υλοποίηση bubblesort µε δείκτες Συνάρτηση swap που της δίνεται προσπέλαση σε στοιχεία του πίνακα Μεµονωµένα στοιχεία του πίνακα: scalars Περνούν ως ορίσµατα by value by default Για να περαστούν by reference χρησιµοποιούµε τον τελεστή διεύθυνσης &

1 // Fig. 5.15: fig05_15.cpp 2 // This program puts values into an array, sorts the values into 3 // ascending order, and prints the resulting array. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 #include <iomanip> 10 11 using std::setw; 12 13 void bubblesort( int *, const int ); // prototype 14 void swap( int * const, int * const ); // prototype 15 16 int main() 17 { 18 const int arraysize = 10; 19 int a[ arraysize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 20 21 cout << "Data items in original order\n"; 22 23 for ( int i = 0; i < arraysize; i++ ) 24 cout << setw( 4 ) << a[ i ]; 25 fig05_15.cpp (1 of 3) 27

26 bubblesort( a, arraysize ); // sort the array 27 28 cout << "\ndata items in ascending order\n"; 29 30 for ( int j = 0; j < arraysize; j++ ) 31 cout << setw( 4 ) << a[ j ]; 32 33 cout << endl; 34 35 return 0; // indicates successful termination 36 37 } // end main 38 39 // sort an array of integers using bubble to sort indicate algorithm function 40 void bubblesort( int *array, const int size ) 41 { 42 // loop to control passes 43 for ( int pass = 0; pass < size - 1; pass++ ) 44 45 // loop to control comparisons during each pass 46 for ( int k = 0; k < size - 1; k++ ) 47 48 // swap adjacent elements if they are out of order 49 if ( array[ k ] > array[ k + 1 ] ) 50 swap( &array[ k ], &array[ k + 1 ] ); Declare as int Receives *array size of array as (rather than argument; int array[]) declared const to ensure size not modified. bubblesort receives single-subscripted array. fig05_15.cpp (2 of 3) 28

51 52 } // end function bubblesort 53 54 // swap values at memory locations to which 55 // element1ptr and element2ptr point 56 void swap( int * const element1ptr, int * const element2ptr ) 57 { 58 int hold = *element1ptr; 59 *element1ptr = *element2ptr; 60 *element2ptr = hold; 61 62 } // end function swap fig05_15.cpp (3 of 3) fig05_15.cpp output (1 of 1) Pass arguments by reference, allowing function to swap values at memory locations. 29 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

Τελεστής sizeof 30 sizeof Μοναδιαίος τελεστής που επιστρέφει το µέγεθος του τελεσταίου σε bytes Γιαπίνακες, το sizeofεπιστρέφει ( size of 1 element ) * ( number of elements ) If sizeof( int ) = 4, then int myarray[10]; cout << sizeof(myarray); θα τυπώσει 40 Το sizeof µπορεί να χρησιµοποιηθεί µε Variable names Type names Constant values

1 // Fig. 5.17: fig05_17.cpp 2 // Demonstrating the sizeof operator. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 char c; 11 short s; 12 int i; 13 long l; 14 float f; 15 double d; 16 long double ld; 17 int array[ 20 ]; 18 int *ptr = array; 19 fig05_17.cpp (1 of 2) 31

20 cout << "sizeof c = " << sizeof c 21 << "\tsizeof(char) = " << sizeof( char ) 22 << "\nsizeof s = " << sizeof s Operator sizeof can be 23 << "\tsizeof(short) = " << sizeof( short ) used on variable Operator name. sizeof fig05_17.cpp can be 24 << "\nsizeof i = " << sizeof i (2 of 2) 25 << "\tsizeof(int) = " << sizeof( int ) used on type name. 26 << "\nsizeof l = " << sizeof l 27 << "\tsizeof(long) = " << sizeof( long ) 28 << "\nsizeof f = " << sizeof f 29 << "\tsizeof(float) = " << sizeof( float ) 30 << "\nsizeof d = " << sizeof d 31 << "\tsizeof(double) = " << sizeof( double ) 32 << "\nsizeof ld = " << sizeof ld 33 << "\tsizeof(long double) = " << sizeof( long double ) 34 << "\nsizeof array = " << sizeof array 35 << "\nsizeof ptr = " << sizeof ptr 36 << endl; 37 38 return 0; // indicates successful termination 39 40 } // end main 32

sizeof c = 1 sizeof(char) = 1 sizeof s = 2 sizeof(short) = 2 sizeof i = 4 sizeof(int) = 4 sizeof l = 4 sizeof(long) = 4 sizeof f = 4 sizeof(float) = 4 sizeof d = 8 sizeof(double) = 8 sizeof ld = 8 sizeof(long double) = 8 sizeof array = 80 sizeof ptr = 4 fig05_17.cpp output (1 of 1) 33

Εκφράσεις και αριθµητική δεικτών 34 Αριθµητική δεικτών Αύξηση/Μείωσηδείκτη (++ or --) Πρόσθεση/αφαίρεση ενός ακεραίου προς/από έναν δείκτη ( + ή +=, -ή-=) Μπορεί να γίνει αφαίρεση µεταξύ δεικτών Η αριθµητική δεικτών είναι άνευ σηµασίας εκτός και εάν πραγµατοποιείται σε δείκτη που αναφέρεται σε πίνακα Έστω ένας πίνακας µε 5 int σε µια µηχανή που αποθηκεύει τους int σε 4 bytes vptr δείχνει το πρώτο στοιχείο v[ 0 ], που βρίσκεται στη θέση 3000 vptr = 3000 vptr += 2; θέται το vptr στη τιµή 3008 vptr δείχνει στο v[ 2 ] location 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4] pointer variable vptr

35 Εκφράσεις και αριθµητική δεικτών Αφαίρεση δεικτών Επιστρέφει το πλήθος των στοιχείων µεταξύ των δύο διευθύνσεων Ανάθεση δεικτών vptr2 = v[ 2 ]; vptr = v[ 0 ]; vptr2 - vptr == 2 Ένας δείκτης µπορεί να ανατεθεί σ έναν άλλο δείκτη αρκεί να είναι του ιδίου τύπου Εάν έχουν διαφορετικό τύπο τότε πρέπει να χρησιµοποιηθεί ο τελεστής cast Εξαίρεση: δείκτηςσε void (type void *) Γενερικός δείκτης, αναπαριστά οποιοδήποτε τύπο εν χρειάζεται casting για να µετατραπεί ένας δείκτης σε void pointer void pointers δεν µπορούν να αποαναφοροποιηθούν

36 Εκφράσεις και αριθµητική δεικτών Σύγκριση δεικτών Με τελεστές ισότητας και σχεσιακούς Η σύγκριση δεικτών είναι άνευ σηµασίας εκτός και εάν οι δείκτες δείχνουν σε στοιχεία του ίδιου πίνακα Συγκρίνονται οι διευθύνσεις που αποθηκεύονται στους δείκτες

37 Σχέση δεικτών και πινάκων Πίνακες και δείκτες συνδέονται στενά Το όνοµα ενός πίνακα είναι ένας σταθερός δείκτης Οι δείκτες µπορούν να πραγµατοποιήσουν την προσπέλαση των στοιχείων ενός πίνακα Προσπέλαση στοιχείων πίνακα µε δείκτες Το στοιχείο b[ n ] µπορεί να προσπελαστεί µε την έκφραση *( bptr + n ) ιεύθυνση &b[ 3 ] ίδιο µε bptr + 3 Το όνοµα ενός πίνακα µπορεί να χρησιµοποιηθεί ως δείκτης b[ 3 ]ίδιοµε *( b + 3 ) Οι δείκτες µπορούν να χρησιµοποιηθούν µε δείκτες bptr[ 3 ] ίδιο µε b[ 3 ]

1 // Fig. 5.20: fig05_20.cpp 2 // Using subscripting and pointer notations with arrays. 3 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 int main() 10 { 11 int b[] = { 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 << "Array b printed with:\n" 16 << "Array subscript notation\n"; 17 18 for ( int i = 0; i < 4; i++ ) 19 cout << "b[" << i << "] = " << b[ i ] << '\n'; 20 21 // output array b using the array name and 22 // pointer/offset notation 23 cout << "\npointer/offset notation where " 24 << "the pointer is the array name\n"; 25 Using array subscript notation. fig05_20.cpp (1 of 2) 38

26 for ( int offset1 = 0; offset1 < 4; offset1++ ) 27 cout << "*(b + " << offset1 << ") = " 28 << *( b + offset1 ) << '\n'; 29 30 // output array b using bptr and array subscript notation 31 cout << "\npointer subscript notation\n"; 32 33 for ( int j = 0; j < 4; j++ ) 34 cout << "bptr[" << j << "] = " << bptr[ j ] << '\n'; 35 36 cout << "\npointer/offset notation\n"; 37 38 // output array b using bptr and pointer/offset notation 39 for ( int offset2 = 0; offset2 < 4; offset2++ ) 40 cout << "*(bptr + " << offset2 << ") = " 41 << *( bptr + offset2 ) << '\n'; 42 43 return 0; // indicates successful termination 44 45 } // end main Using array name and pointer/offset notation. Using bptr and pointer/offset notation. fig05_20.cpp (2 of 2) Using pointer subscript notation. 39

Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 fig05_20.cpp output (1 of 1) 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

1 // Fig. 5.21: fig05_21.cpp 2 // Copying a string using array notation 3 // and pointer notation. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 void copy1( char *, const char * ); // prototype 10 void copy2( char *, const char * ); // prototype 11 12 int main() 13 { 14 char string1[ 10 ]; 15 char *string2 = "Hello"; 16 char string3[ 10 ]; 17 char string4[] = "Good Bye"; 18 19 copy1( string1, string2 ); 20 cout << "string1 = " << string1 << endl; 21 22 copy2( string3, string4 ); 23 cout << "string3 = " << string3 << endl; 24 25 return 0; // indicates successful termination fig05_21.cpp (1 of 2) 41

26 27 } // end main 28 29 // copy s2 to s1 using array notation 30 void copy1( char *s1, const char *s2 ) 31 { 32 for ( int i = 0; ( s1[ i ] = s2[ i ] )!= '\0'; i++ ) 33 ; // do nothing in body 34 35 } // end function copy1 36 37 // copy s2 to s1 using pointer notation 38 void copy2( char *s1, const char *s2 ) 39 { in s1. 40 for ( ; ( *s1 = *s2 )!= '\0'; s1++, s2++ ) 41 ; // do nothing in body 42 43 } // end function copy2 string1 = Hello string3 = Good Bye Use array subscript notation to copy string in s2 to character array s1. Use pointer notation to copy string in s2 to character array fig05_21.cpp (2 of 2) Increment both pointers to point to next elements in corresponding arrays. fig05_21.cpp output (1 of 1) 42

43 Πίνακες δεικτών Οι πίνακες µπορεί να περιέχουν δείκτες Π.χ. να αποθηκεύσουν έναν πίνακα µε strings char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" }; Κάθε στοιχείο του suit δείχνει σε char * (a string) Ο πίνακας δεν αποθηκεύει strings, µόνο δείκτες σε strings suit[0] H e a r t s \0 suit[1] suit[2] suit[3] D i a m o n d s \0 C l u b s \0 S p a d e s \0 Οπίνακας suitέχεισταθερόµέγεθος, αλλάτα strings µπορεί να είναι οποιουδήποτε µεγέθους

44 είκτες σε συναρτήσεις είκτες συναρτήσεων Περιέχουν τη διεύθυνση µιας συνάρτησης Όπως το όνοµα ενός πίνακα δείχνει στο πρώτο στοιχείο του πίνακα Το όνοµα µιας συνάρτησης δείχνει στην αρχική διεύθυνση της µνήµης που αποθηκεύει τον κώδικα ορισµού της συνάρτησης Οι δείκτες συναρτήσεων µπορεί Να περάσουν σε συναρτήσεις Να επιστραφούν από συναρτήσεις Να αποθηκευθούν σε πίνακες Να ανατεθούν σε άλλους δείκτες συναρτήσεων

45 είκτες συναρτήσεων Κλήση συναρτήσεων µε δείκτες Έστω: bool ( *compare ) ( int, int ) Εκτέλεση της συνάρτησης είτε µε ( *compare ) ( int1, int2 ) Αποαναφορά του δείκτη σε συνάρτηση είτε µε compare( int1, int2 ) Μπορεί να προκαλέσει σύγχυση Ο χρήστης µπορεί να νοµίζει ότι compare είναι το όνοµα της συνάρτησης στο πρόγραµµα

1 // Fig. 5.25: fig05_25.cpp 2 // Multipurpose sorting program using function pointers. 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <iomanip> 10 11 using std::setw; 12 13 // prototypes 14 void bubble( int [], const int, bool (*)( int, int ) ); 15 void swap( int * const, int * const ); 16 bool ascending( int, int ); 17 bool descending( int, int ); 18 19 int main() 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 fig05_25.cpp (1 of 5) Parameter is pointer to function that receives two integer parameters and returns bool result. 46

26 cout << "Enter 1 to sort in ascending order,\n" 27 << "Enter 2 to sort in descending order: "; 28 cin >> order; 29 cout << "\ndata items in original order\n"; 30 31 // output original array 32 for ( counter = 0; counter < arraysize; counter++ ) 33 cout << setw( 4 ) << a[ counter ]; 34 35 // sort array in ascending order; pass function ascending 36 // as an argument to specify ascending sorting order 37 if ( order == 1 ) { 38 bubble( a, arraysize, ascending ); 39 cout << "\ndata items in ascending order\n"; 40 } 41 42 // sort array in descending order; pass function descending 43 // as an agrument to specify descending sorting order 44 else { 45 bubble( a, arraysize, descending ); 46 cout << "\ndata items in descending order\n"; 47 } 48 fig05_25.cpp (2 of 5) 47

49 // output sorted array 50 for ( counter = 0; counter < arraysize; counter++ ) 51 cout << setw( 4 ) << a[ counter ]; 52 53 cout << endl; 54 55 return 0; // indicates successful termination 56 57 } // end main 58 59 // multipurpose bubble sort; parameter compare is a pointer to 60 // the comparison function that determines sorting order 61 void bubble( int work[], const int size, 62 bool (*compare)( int, int ) ) 63 { 64 // loop to control passes compare is pointer to function that receives two integer parameters and returns bool result. Parentheses necessary to indicate pointer to function 65 for ( int pass = 1; pass < size; pass++ ) 66 67 // loop to control number of comparisons per pass 68 for ( int count = 0; count < size Call -passed 1; count++ function ) 69 compare; dereference 70 // if adjacent elements are pointer out of to order, execute swap function. them 71 if ( (*compare)( work[ count ], work[ count + 1 ] ) ) 72 swap( &work[ count ], &work[ count + 1 ] ); fig05_25.cpp (3 of 5) 48

73 74 } // end function bubble 75 76 // swap values at memory locations to which 77 // element1ptr and element2ptr point 78 void swap( 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, int b ) 89 { 90 return b < a; // swap if b is less than a 91 92 } // end function ascending 93 fig05_25.cpp (4 of 5) 49

94 // determine whether elements are out of order 95 // for a descending order sort 96 bool descending( int a, int b ) 97 { 98 return b > 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 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 fig05_25.cpp (5 of 5) fig05_25.cpp output (1 of 1) 50 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

51 είκτες συναρτήσεων Πίνακες µε δείκτες σε συναρτήσεις Menu-driven systems είκτες για κάθε συνάρτηση αποθηκεύονται σε πίνακα µε δείκτες σε συναρτήσεις Όλες οι συναρτήσεις πρέπει να έχουν τον ίδιο επιστρεφόµενο τύπο και τους ίδιους τύπους παραµέτρων Επιλογή µενού δείκτης σε πίνακα µε δείκτες συναρτήσεων

1 // Fig. 5.26: fig05_26.cpp 2 // Demonstrating an array of pointers to functions. 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function prototypes 10 void function1( int ); 11 void function2( int ); 12 void function3( int ); 13 14 int main() 15 { 16 // initialize array of 3 pointers to functions that each 17 // take an int argument and return void names are pointers. 18 void (*f[ 3 ])( int ) = { function1, function2, function3 }; 19 20 int choice; 21 22 cout << "Enter a number between 0 and 2, 3 to end: "; 23 cin >> choice; 24 Array initialized with names of three functions; function fig05_26.cpp (1 of 3) 52

25 // process user's choice 26 while ( choice >= 0 && choice < 3 ) { 27 28 // invoke function at location choice in array f 29 // and pass choice as an argument 30 (*f[ choice ])( choice ); 31 32 cout << "Enter a number between 0 and 2, 3 to end: "; 33 cin >> choice; 34 } Call chosen function by 35 36 cout << "Program execution completed." element << in endl; array. 37 38 return 0; // indicates successful termination 39 40 } // end main 41 42 void function1( int a ) 43 { 44 cout << "You entered " << a 45 << " so function1 was called\n\n"; 46 47 } // end function1 48 dereferencing corresponding fig05_26.cpp (2 of 3) 53

49 void function2( int b ) 50 { 51 cout << "You entered " << b 52 << " so function2 was called\n\n"; 53 54 } // end function2 55 56 void function3( int c ) 57 { 58 cout << "You entered " << c 59 << " so function3 was called\n\n"; 60 61 } // end function3 fig05_26.cpp (3 of 3) fig05_26.cpp output (1 of 1) 54 Enter a number between 0 and 2, 3 to end: 0 You entered 0 so function1 was called Enter a number between 0 and 2, 3 to end: 1 You entered 1 so function2 was called Enter a number between 0 and 2, 3 to end: 2 You entered 2 so function3 was called Enter a number between 0 and 2, 3 to end: 3 Program execution completed.

55 Συναρτήσεις διαχείρισης συµβολοσειρών Η βιβλιοθήκη <cstring> παρέχει συναρτήσεις για ιαχείριση δεδοµένων τύπου string Σύγκριση των strings Αναζήτηση χαρακτήρων ή άλλων strings Tokenize strings (διαχωρισµός των strings σε λογικές µοναδες)

56 Συναρτήσεις διαχείρισης συµβολοσειρών char *strcpy( char *s1, const char *s2 ); char *strncpy( char *s1, const char *s2, size_t n ); char *strcat( char *s1, const char *s2 ); char *strncat( char *s1, const char *s2, size_t n ); Copies the string s2 into the character array s1. The value of s1 is returned. Copies at most n characters of the string s2 into the character array s1. The value of s1 is returned. Appends the string s2 to the string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. Appends at most n characters of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively.

Συναρτήσεις διαχείρισης συµβολοσειρών 57 int strncmp( const char *s1, const char *s2, size_t n ); char *strtok( char *s1, const char *s2 ); size_t strlen( const char *s ); Compares up to n characters of the string s1 with the string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. A sequence of calls to strtok breaks string s1 into tokens logical pieces such as words in a line of text delimited by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned. Determines the length of string s. The number of characters preceding the terminating null character is returned.

1 // Fig. 5.28: fig05_28.cpp 2 // Using strcpy and strncpy. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <cstring> // prototypes for strcpy and strncpy 9 10 int main() 11 { 12 char x[] = "Happy Birthday to You"; 13 char y[ 25 ]; 14 char z[ 15 ]; 15 <cstring> contains prototypes for strcpy and strncpy. Copy entire string in array x into array y. 16 strcpy( y, x ); // copy contents of x into y 17 18 cout << "The string in array x is: " << x 19 << "\nthe string in array y is: " << y << '\n'; 20 21 // copy first 14 characters of x into character. z 22 strncpy( z, x, 14 ); // does not copy null character 23 z[ 14 ] = '\0'; // append '\0' to z's contents 24 25 cout << "The string in array z is: " << z << endl; Copy first 14 characters of Append array terminating x into array null y. Note that this does not write terminating null character. fig05_28.cpp (1 of 2) 58

26 27 return 0; // indicates successful termination 28 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 String to copy. Copied string using strcpy. Copied first 14 fig05_28.cpp characters using strncpy. (2 of 2) fig05_28.cpp output (1 of 1) 59

1 // Fig. 5.29: fig05_29.cpp 2 // Using strcat and strncat. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <cstring> // prototypes for strcat and strncat 9 10 int main() 11 { 12 char s1[ 20 ] = "Happy "; 13 char s2[] = "New Year "; 14 char s3[ 40 ] = ""; Append s2 to s1. 15 16 cout << "s1 = " << s1 << "\ns2 = " << s2; 17 18 strcat( s1, s2 ); // concatenate s2 to s1 19 20 cout << "\n\nafter strcat(s1, s2):\ns1 = " << s1 21 << "\ns2 = " << s2; 22 <cstring> contains prototypes for strcat and strncat. Append first 6 characters of s1 to s3. 23 // concatenate first 6 characters of s1 to s3 24 strncat( s3, s1, 6 ); // places '\0' after last character 25 fig05_29.cpp (1 of 2) 60

26 cout << "\n\nafter strncat(s3, s1, 6):\ns1 = " << s1 Append s1 to s3. 27 << "\ns3 = " << s3; 28 29 strcat( s3, s1 ); // concatenate s1 to s3 30 cout << "\n\nafter strcat(s3, s1):\ns1 = " << s1 31 << "\ns3 = " << s3 << endl; 32 33 return 0; // indicates successful termination 34 35 } // end main fig05_29.cpp (2 of 2) fig05_29.cpp output (1 of 1) 61 s1 = Happy s2 = New Year After strcat(s1, s2): s1 = Happy New Year s2 = New Year After strncat(s3, s1, 6): s1 = Happy New Year s3 = Happy After strcat(s3, s1): s1 = Happy New Year s3 = Happy Happy New Year

1 // Fig. 5.30: fig05_30.cpp 2 // Using strcmp and strncmp. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 <cstring> contains prototypes for strcmp and strncmp. 12 #include <cstring> // prototypes for strcmp and strncmp 13 14 int main() 15 { 16 char *s1 = "Happy New Year"; 17 char *s2 = "Happy New Year"; 18 char *s3 = "Happy Holidays"; 19 20 cout << "s1 = " << s1 << "\ns2 = " << s2 21 << "\ns3 = " << s3 << "\n\nstrcmp(s1, s2) = " 22 << setw( 2 ) << strcmp( s1, s2 ) 23 << "\nstrcmp(s1, s3) = " << setw( 2 ) 24 << strcmp( s1, s3 ) << "\nstrcmp(s3, s1) = " 25 << setw( 2 ) << strcmp( s3, s1 ); Compare s1 and s3. Compare s1 and s2. Compare s3 and s1. fig05_30.cpp (1 of 2) 62

Compare up to 6 characters of Compare up to 7 characters of 26 27 cout << "\n\nstrncmp(s1, s3, 6) = " << s1 setw( and 2 s3. ) 28 << strncmp( s1, s3, 6 ) << "\nstrncmp(s1, s1 and s3. s3, 7) = " 29 << setw( 2 ) << strncmp( s1, s3, 7 ) 30 << "\nstrncmp(s3, s1, 7) = " s3 and s1. 31 << setw( 2 ) << strncmp( s3, s1, 7 ) << endl; 32 33 return 0; // indicates successful termination 34 35 } // end main Compare up to 7 characters of fig05_30.cpp (2 of 2) fig05_30.cpp output (1 of 1) 63 s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays strcmp(s1, s2) = 0 strcmp(s1, s3) = 1 strcmp(s3, s1) = -1 strncmp(s1, s3, 6) = 0 strncmp(s1, s3, 7) = 1 strncmp(s3, s1, 7) = -1

1 // Fig. 5.31: fig05_31.cpp 2 // Using strtok. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <cstring> // prototype for strtok 9 10 int main() 11 { <cstring> contains prototype for strtok. 12 char sentence[] = "This is a sentence with 7 tokens"; 13 char *tokenptr; 14 15 cout << "The string to be tokenized is:\n" << sentence 16 << "\n\nthe tokens are:\n\n"; 17 tokenization. 18 // begin tokenization of sentence 19 tokenptr = strtok( sentence, " " ); 20 First call to strtok begins fig05_31.cpp (1 of 2) 64

21 // continue tokenizing sentence until tokenptr becomes NULL 22 while ( tokenptr!= NULL ) { 23 cout << tokenptr << '\n'; 24 tokenptr = strtok( NULL, " " ); // get next token 25 26 } // end while 27 28 cout << "\nafter strtok, sentence = " << sentence << endl; 29 30 return 0; // indicates successful termination 31 32 } // end main Subsequent calls to strtok with NULL as first argument to indicate continuation. fig05_31.cpp (2 of 2) 65

The string to be tokenized is: This is a sentence with 7 tokens 66 The tokens are: This is a sentence with 7 tokens fig05_31.cpp output (1 of 1) After strtok, sentence = This

67 Αναφορές Harvey M. Deitel, Paul J. Deitel, C++ How to Program, 4th Edition, Prentice Hall. Bjarne Stroustrup, The C++ Programming Language, Special Edition, Addison-Wesley.

Άσκηση-1 68 Τι κάνει το διπλανό πρόγραµµα?

Άσκηση-2 69 Τι κάνει το διπλανό πρόγραµµα?

Άσκηση-3 70 Τι κάνει το διπλανό πρόγραµµα?

Απάντηση-1 71 Συνενώνει τα δύο string που δίνονται στην είσοδο και τα τυπώνει

Απάντηση-2 72 Τυπώνει το µήκος του string

Απάντηση-3 73 Ελέγχει την ισότητα των δύο string εισόδου