1 / 24

Lecture 28: CStrings & POinters

CSC 107 – Programming For Science. Lecture 28: CStrings & POinters. 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

gudrun
Download Presentation

Lecture 28: CStrings & POinters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 107 – Programming For Science Lecture 28:CStrings & POinters

  2. 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

  3. Arrays vs. Pointers Arrays Pointers Yams Sweet Potatoes

  4. Arrays vs. Pointers Arrays Pointers Yams Sweet Potatoes Sweet Potatoes Yams

  5. Arrays vs. Pointers Arrays = YamsSweet 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

  6. 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

  7. 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 used char* strstr(const char*, const char*);char* strchr(const char*, intchr);

  8. Using the strstrFunction • Finds where 2ndcString is found in 1stcString • 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 foundchar 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");

  9. 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

  10. 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)

  11. 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;

  12. 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;

  13. 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;

  14. 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;

  15. 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;

  16. 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;

  17. 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;

  18. 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;

  19. 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;

  20. 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;

  21. 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;

  22. Using the strchrFunction • Finds where charfound in 1stcString • 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 foundchar 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');

  23. Your Turn • Get into your groups and try this assignment

  24. 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.

More Related