370 likes | 507 Views
CSE 2341 Object Oriented Programming with C++ Note Set #2. Quick Look. Review of Declaration, Initialization and use of pointers and arrays of pointers Creation of c-strings Use of built-in string functions. Address Operator - &. int amount = 25; & amount;.
E N D
Quick Look • Review of • Declaration, Initialization and use of pointers and arrays of pointers • Creation of c-strings • Use of built-in string functions
Address Operator - & int amount = 25; &amount; • & returns the memory address of where variable amount is stored in • the computers memory • cout << &amount would print thememory address to the console
Pointer Declaration T*name; Any previously defineddata type Indicates declarationof pointer variable Declares a pointer variable that can store the address of a memory location that stores data of type T. Note: T is used to represent any valid data typein a great deal of C++ literature.
Example int main(){ int a; // a is an integer int *aPtr; // aPtr is a pointer to //an integer a = 7; aPtr = &a; // aPtr set to address of a cout << a; // displays 7 cout << aPtr; // displays the memory // location of a cout << *aPtr; // displays 7 return 0; }
Pass by Reference with Pointer Parameter void cubeByRef(int *); int main() { int number = 5; cout << “Original Value = “ << number << endl; cubeByRef(&number); cout << “New Value = “ << number << endl; return 0; }
cubeByRef(int *) 5 Function Call From Main: cubeByRef(&number); number (stored at 3fe0) provides the address of number to pointer variablepVar. 3fe0 pVar (stored at 3fe0) void cubeByRef(int* pVar) { *pVar = *pVar * *pVar * *pVar; }
Converting Upper- to Lowercase void convertUp(char *); int main() { char str[] = “chars and $32.22”; cout << “Before: “ << str << endl; convertUp(str); cout << “After: “ << str << endl; return 0; }
convertUp(char *) chars and $32.22 Chars and $32.22 str from main void convertUp(char * sPtr) { while(*sPtr != ‘\0’) { if(islower(*sPtr)) *sPtr = toupper(*sPtr) ++sPtr; //move to next character } } address of ‘c’ address of ‘h’ sPtr
convertUp(char *) Chars and $32.22 CHars and $32.22 str from main void convertUp(char * sPtr) { while(*sPtr != ‘\0’) { if(islower(*sPtr)) *sPtr = toupper(*sPtr) ++sPtr; //move to next character } } address of ‘c’ address of ‘a’ sPtr
convertUp(char *) CHars and $32.22 CHArs and $32.22 str from main void convertUp(char * sPtr) { while(*sPtr != ‘\0’) { if(islower(*sPtr)) *sPtr = toupper(*sPtr) ++sPtr; //move to next character } } address of ‘c’ address of ‘r’ sPtr
const-ness with pointers • const disallows changes to values • checked by the compiler int x = 5, y; const int * const myPtr = &x; *myPtr = 7; ptr = &y; //Illegal //Illegal
Const-ness with pointers int x = 5, y; const int * const myPtr = &x; *myPtr = 7; ptr = &y; Disallows modification of the value pointed to by myPtr Disallows modification of the address stored in myPtr
Arrays and Pointers int arr[] = {10, 11, 12, 13}; int* ptr; • arr holds the address of the first element of the array (10) ptr = arr; • Now, ptr and arr both point to the first • element of the array (10)
Subscript vs. Pointer Offset Notation int arr[] = {10, 11, 12, 13}; int* ptr; ptr = arr; ptr //address of 10 ptr +2 //address of ____ *(ptr + 2) //value ____
Subscript vs. Pointer Offset Notation int arr[] = {10, 11, 12, 13}; int* ptr; ptr = arr; for (int i = 0; i < 4; i++) { cout << arr[i] << endl; cout << *(arr + i) << endl; cout << *(ptr + i) << endl; cout << ptr[i] << endl; } 0 i Will all display 10
Subscript vs. Pointer Offset Notation int arr[] = {10, 11, 12, 13}; int* ptr; ptr = arr; for (int i = 0; i < 4; i++) { cout << arr[i] << endl; cout << *(arr + i) << endl; cout << *(ptr + i) << endl; cout << ptr[i] << endl; } 1 i Will all display 11
Copying Strings void copy1(char*, const char*); void copy2(char*, const char*); int main() { char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy1(s1, s2); cout << “s1 = “ << s1 << endl; copy2(s3, s4); cout << “s3 = “ << s3 << endl; return 0; }
copy1(char*, const char*) //FROM MAIN: char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy1(s1, s2); void copy1(char* a, const char* b) { for(int i=0; (a[i]=b[i])!=‘\0’; i++); } Body of for loop is empty. Action happens in the condition check.
copy1(char*, const char*) //FROM MAIN: char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy1(s1, s2); void copy1(char* a, const char* b) { for(int i=0; (a[i]=b[i])!=‘\0’; i++); } H ? e ? l ? ? l o ? ? \0 ? ? ? ?
copy2(char*, const char*) //FROM MAIN: char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy2(s3, s4); void copy1(char* a, const char* b) { for(; (*a = *b)!=‘\0’; a++, b++); } G ? o ? ? o d ? b ? ? y e ? \0 ? ? ?
Array of Pointers to Strings const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” }; suit H e a r t s \0 C l u b s \0 D i a m o n d s \0 S p a d e s \0 cout << suit[2]+4; _________ cout << *(*suit + 1); _________
Arrays of Pointers to Strings const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” }; cout << suit[2]+4; _________ cout << *(*suit + 1); _________ cout << suit[0]; _________ cout << *(suit[3] + 3); _________
String Functions • strcpy(string1,string2);// copies entire contents(including the null) of // string2 to string1, replacing anything that is// in string1; • strncpy(string1,string2,5);// copies at most 5 characters from string2 to// the beginning of string1; null is not copied;// programmer must place the null in string1 • strcat(string1,string2);// appends the entire string2 string to the end// of the string1 beginning at the null
String Functions const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” }; char str[25]; char * strPtr; strcpy(str, suit[1]); strcat(str, suit[3]); cout << str << endl; strncpy(strPtr, suit[0], 3); cout << strPtr << endl; Any problems with this code? What will be displayed?
Comparing Strings char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(s1 == s2) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl; Output: Strings not equal WHY??
Comparing Strings char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(s1 == s2) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl; Output: Strings not equal WHY??
String Compare - strcmp • strcmp(s1, s2) returns • a negative number if s1 is smaller than s2 • a positive number if s1 is larger than s2 • 0 if s1 is the same as s2 char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strcmp(s1, s2) == 0) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl;
String Compare - strncmp • strncmp(s1, s2, x) returns • a negative number if s1 is smaller than s2 for x characters • a positive number if s1 is larger than s2 for x characters • 0 if s1 is the same as s2 for x characters char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strncmp(s1+1, s3+1, 4) == 0) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl;
String Compare - strncmp char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strncmp(s1, s3, 6) == 0) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl;
String Compare - strncmp char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strcmp(s1, s3) == 0) cout << “Strings equal” << endl; else if(strcmp(s1, s3) < 0) cout << “S1 < S3” << endl; else cout << “S1 > S3” << endl;
strcmp • Review: • strcmp returns a negative number if string1 is less than string2 (not a -1) • Strcmp returns a positive number is string1 is greater than string2 (not a +1 • Strcmp returns 0 if string1 is equal to string2
strtok • String tokenizing means to break a string up into component tokens using a delimiter • Ex: string = helloworld delimiter = w tokens = hello, orld
strtok • First call in a sequence of code to the function strtok contains two arguments • The string to be tokenized • A string containing characters that separate the tokens (delimiters) char* str = “This is a sentence, with 7 tokens”; char* delim = “ ,”; char* tokenPtr; tokenPtr = strtok(str, delim);
strtok • When strtok can find no more tokens, it will return NULL char* string=“This is a sentence,with 7 tokens”;char *tokenPtr;tokenPtr = (string,” ,“); while (tokenPtr != NULL){ cout << tokenPtr << ‘\n’; tokenPtr = strtok(NULL, “ ,”); } Output: Thisisasentencewith7tokens
strtok • Subsequent calls to strtok • arg1: NULL • arg2: string of delimiters (does not have to be the same as the set of delims for the 1st call) char* str = “This is a sentence, with 7 tokens”; char* delim = “ ,”; char* tokenPtr; tokenPtr = strtok(str, delim); cout << tokenPtr << endl; tokenPtr = strtok(NULL, delim); cout << tokenPtr << endl;
Fini ?