Download presentation
Presentation is loading. Please wait.
Published byRosamond Carroll Modified over 9 years ago
1
CSC 107 – Programming For Science
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 hardest topic of term Pointers still hard & this is hardest topic of term
3
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 hardest topic of term Pointers still hard & this is hardest topic of term (Unless you have not learned how to write loops, yet)
4
Pointers & Variables Variable gives name for address in memory Initial values unknown if it is not set Value at the location used when variable used Pointer is variable like any other… … but the pointer’s value is address of a variable Aliases other variables so both can update location Pointers can be used like other variables… …but value is address and not a useful number
5
& and * Operators variable & operator gets address of scalar variable Use only with variables Use only with variables, including array elements Lets variable & pointer alias same memory location Pointer used two ways: arrow & target of arrow When code has the name alone, using the arrow With *pointer, value at target can be used or set double x = 4, *y, *z; y = &x; z = y; cout << x << " " << y << " " << *y << endl; cout << x << " " << y << " " << *y << endl;
6
Arrays vs. Pointers ArraysPointers YamsSweet Potatoes
7
Arrays vs. Pointers Arrays = YamsPointers = Sweet Potatoes 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
8
Arrays vs. Pointers Arrays = YamsPointers = Sweet Potatoes 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 use pointers & arrays interchangeably
9
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
10
How Pointers Get Used
11
Using the strstr Function Finds where 2 nd cString is found in 1 st cString Pointer to 1 st 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");
12
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 & must be in CAPS
13
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 & must be in CAPS (Tony apologizes,btw)
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 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;
23
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;
24
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;
25
Using the strchr Function Finds where char found in 1 st cString Pointer to 1 st 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');
26
Your Turn Get into your groups and try this assignment
27
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 #11 due Tues. Last programming assignment Last programming assignment available now
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.