Lecture 28: CStrings & POinters
description
Transcript of Lecture 28: CStrings & POinters
![Page 1: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/1.jpg)
LECTURE 28:CSTRINGS & POINTERS
CSC 107 – Programming For Science
![Page 2: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/2.jpg)
Today’s Goal
Learn how pointers really used with arrays Exploit the similarity between pointers &
arrays Take advantage of relationship with built-in
functions Understand what NULL is & why we use
NULL
Pointers still hard & this is hardest topic of term
![Page 3: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/3.jpg)
Arrays vs. Pointers
Arrays Pointers
Yams Sweet Potatoes
![Page 4: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/4.jpg)
Arrays vs. Pointers
Arrays Pointers
YamsSweet Potatoes
Sweet PotatoesYams
![Page 5: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/5.jpg)
Arrays vs. Pointers
Arrays = Yams Sweet Potatoes
Pointers = Sweet Potatoes Yams
Makes space at declaration
Variable value is address
Use [] to access entries Can also access using *
Can be assigned a pointer
Needs target to be useful
Variable value is address
Use * to access target Can also access using []
Can be assigned array
Often usepointers & arrays interchangeably
![Page 6: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/6.jpg)
Arrays + Pointers =
Pointer arithmetic enables mixing & matching Relies on fact that * and [] are synonyms Used processing cStrings (& with other
arrays, too) Provides another way to access array
elements Use foo[nnn] to access entry in foo at
index nnn But could also access entry using *(foo+nnn)
Pointers follow same rules and work similarly But, pointers access offset from where they
point
![Page 7: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/7.jpg)
How Pointers Get Used
Lots of built-in cString functions return pointers Not a new array, but points within array
passed in Provides a way to use & modify cString as
needed Requires program add line to include
prototypes#include <cstring>
Two main functions that are often usedchar* strstr(const char*, const char*);char* strchr(const char*, int chr);
![Page 8: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/8.jpg)
Using the strstr Function
Finds where 2nd cString is found in 1st cString Pointer to 1st entry returned by this function To be found, needs perfect match of entire
cString Looks from left to right until a match is
found
char myName[]="John J. Jingleheimerschmidt";char *start = strstr(myName, "J");char *ptr = strstr(myName, "John");char *middle = strstr(start + 1, "J");char *last = strstr(middle + 1, "J");char *oops = strstr(last + 1, "J");
![Page 9: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/9.jpg)
Slide Is NULL (But Not void) NULL is special value used by all
pointers Not an actual address – NULL used if when
no target Not a real address, but says DO NOT USE
ME If pointer is NULL, accessing value will
crash May get vague error like "Segmentation
Fault" Most likely, however, the program simply
crashes Use NULL anywhere in code pointer is
usable Needs #include <cstring> & must be in
CAPS
![Page 10: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/10.jpg)
Slide Is NULL (But Not void) NULL is special value used by all
pointers Not an actual address – NULL used if when
no target Not a real address, but says DO NOT USE
ME If pointer is NULL, accessing value will
crash May get vague error like "Segmentation
Fault" Most likely, however, the program simply
crashes Use NULL anywhere in code pointer is
usable Needs #include <cstring> & must be in
CAPS (Tony apologizes,btw)
![Page 11: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/11.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 12: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/12.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 13: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/13.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 14: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/14.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 15: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/15.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 16: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/16.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 17: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/17.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 18: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/18.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 19: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/19.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 20: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/20.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr); *ptr = NULL;
![Page 21: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/21.jpg)
Using NULL With Pointers
char * ptr = NULL;char *prof = "Matthew Hertz"; char letter = *ptr;float * toiletBulb = ptr;toiletBulb = NULL;int * pIX = null;ptr = strstr("Hi Mom!", "mom");strstr(NULL, "Monkeys!");strstr(prof, ptr);*ptr = NULL;
![Page 22: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/22.jpg)
Using the strchr Function
Finds where char found in 1st cString Pointer to 1st entry returned by this function To be found, needs exact match of the char Looks from left to right until a match is
found
char myName[]="John J. Jingleheimerschmidt";char *start = strchr(myName, 'J');char *ptr = strchr(myName, myName[4]);char *startOver = strchr(start, 'J');char *middle = strchr(start + 1, 'J');char *nope = strchr(myName, 'j');
![Page 23: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/23.jpg)
Your Turn
Get into your groups and try this assignment
![Page 24: Lecture 28: CStrings & POinters](https://reader035.fdocuments.us/reader035/viewer/2022081520/568162e0550346895dd3650a/html5/thumbnails/24.jpg)
For Next Lecture
Dynamic memory allocation in 12.9 & 12.17 More interesting topic in all of computer
science? As program runs, can we grow & shrink
arrays? My thesis was related to which lecture this
semester?
Angel also has Weekly Assignment #10 due Wed.