Download presentation
Presentation is loading. Please wait.
Published byBethany Richard Modified over 9 years ago
1
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ
2
Administrative stuff Reminder: No class on Friday. Celebrate the US Independence. Dress in Stripes and stars.... Yell “ ‘merica” at everyone and have fun, but be safe. Also no Quiz, celebrate that too. Due date for Homework #4 is now Monday. Test cases will be posted tonight. Homework #3 grades are out. If you got a 20 with a message about compilation GO TALK TO THE TA ASAP. Questions?
3
Pointers We started by talking about indirection Remember? I asked you to pray for me (that’s called intercessory prayer, and it’s cool) We were talking about defining pointers. We talked about the & operator We talked about the * operator But…. Why the heck would someone want pointers????
4
Let’s talk about applications! Let’s say we are coding a management system for a course. We have a struct that saves all the info for a student (name, UFID, DOB, major, year, etc). struct student{ char *name; int UFID; char major[3]; struct Date DOB; int year; }; We need to store all the students in the class With what we know right now. How would you store the students? Arrays?
5
Array of students AndrewMichaelCristinaAnaLaura [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; …
6
Array of students AndrewMichaelCristinaAnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … What if Cristina decides to drop the class? How do we handle that in code?
7
Array of students AndrewMichaelCristinaAnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … What if Cristina decides to drop the class? How do we handle that in code? 0 1 2 3 4 49
8
Array of students AndrewMichael[Empty]AnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 1: we just add an empty space… clear the struct for Cristina, and we are done. Drawbacks: 1.all our functions on the list, would have to account for possible empty spaces before the end of the array. 2.We always have to visit the whole array instead of just the active students. 0 1 2 3 4 49
9
Array of students AndrewMichael[Empty]AnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 2: We can move the last student (Michael) to Cristina’s space…. 0 1 2 3 4 49
10
Array of students Andrew[Empty]MichaelAnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 2: We can move the last student (Michael) to Cristina’s space…. Drawbacks: We lose alphabetical order. We would need to sort. 0 1 2 3 4 49
11
Array of students AndrewMichael[Empty]AnaJuliana [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 3: we move everyone just one slot. 0 1 2 3 4 49
12
Array of students Andrew[Empty]JulianaAnaMichael [Empty] arrayOfStudents[50] struct student arrayOfStudents[50]; … Option 3: we move everyone just one slot. Drawback: Sort of expensive… What if it had been Ana? We have to move EVERY SINGLE student… 0 1 2 3 4 49
13
Pointers allows us to model lists… A list is a data structure. We haven’t discussed the concept, because it’s not the focus of this class. Data structures are ways to organize data in efficient ways (computationally speaking).
14
Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?
15
Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?
16
Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?
17
Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?
18
Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewCristinaJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?
19
Lists We will definitely implement a List next week. But essentially they look like this: AnaAndrewJulianaMichael What if Cristina decides to drop the class? How do we handle that in code?
20
Lists So lists are cool… AnaAndrewCristinaJulianaMichael
21
Lists So lists are cool… while for the visualization seeing it so linearly made so much sense… the truth is that in memory…. The list is all over the place (usually) AnaAndrewCristinaJulianaMichael
22
Lists So lists are cool… while for the visualization seeing it so linearly made so much sense… the truth is that in memory…. The list is all over the place (usually)
23
Lists So lists are cool… while for the visualization seeing it so linearly made so much sense… the truth is that in memory…. The list is all over the place (usually) Ana Andrew Cristina Juliana Michael
24
Other uses of pointers? Files! We haven’t worked with those yet. Wonder why? We usually upload a file to memory, and they are all encoded differently. Different programs have different formats that are not interchangeable. Doc, docx,pdf,txt,ppt,pptx,xls,c, …
25
Other uses of pointers? Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory.
26
Other uses of pointers? Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory. That includes: code! Now let’s say we have multiple methods that do similar things, but they have the same parameters… Examples? Sort methods Math operations Sets of problems that we can only approximate, and there are multiple approximation methods.
27
Other uses of pointers? Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A(); }else if(input == 'B‘) { B(); }else if(input == 'C') { C(); } A B C All of them are functions (pieces of code) and they do similar functionality
28
Pointers to functions Calling functions without knowing their name…. (ok you don’t need to understand this, but it IS beautiful) A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A( ); }else if(input == 'B‘) { B( ); }else if(input == 'C') { C( ); } A B C All of them are functions (pieces of code) and they do similar functionality
29
Pointers to functions A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A( ); }else if(input == 'B‘) { B( ); }else if(input == 'C') { C( ); } Let’s say someone really smart, comes up with method D, and wants to make it available on your program to compare against A,B,C. A B C D
30
Pointers to functions A pointer can point to anything in memory. That includes: code! if(input == 'A' ) { A( ); }else if(input == 'B‘) { B( ); }else if(input == 'C') { C( ); }else if(input == 'D'){ D( ); } A B C D That’s a bit annoying. Having to change the code… What if the person That wrote D doesn’t have the original code?
31
Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers
32
Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers
33
Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers
34
Pointers to functions A pointer can point to anything in memory. That includes: code! Let’s say we have a “magic” pointer ptr. We could have this code: if(ptr != NULL) { ptr( ); } A B C D ptr I’ll talk about the specific type of ptr next week. For now just believe me: it is possible to this with pointers E Z … This sort of idea is VERY common in optimization software. Matlab uses it quite a bit.
35
Let’s go back to the simple stuff…. Memory./a.out When we execute (e.g. %./a.out) instructions Program Counter (PC)
36
Let’s go back to the simple stuff…. Memory./a.out As execution continues, the PC points to the next instruction to execute. instructions Program Counter (PC)
37
Let’s go back to the simple stuff…. Memory./a.out When we reach variable definitions like this one: int i = 0; instructions Program Counter (PC) 0 i int i = 0;
38
Ok… now pointers? Memory./a.out When we reach pointer definitions like this one: int *prt_i = 0; instructions Program Counter (PC) 0 i int *prt_i = 0 ptr_i
39
Ok… now pointers? Memory./a.out When we reach pointer definitions like this one: int *prt_i = NULL; instructions Program Counter (PC) 0 i int *prt_i = NULL ptr_i
40
Ok… now pointers? Memory./a.out When we use & and apply it to i &i (in any context….) instructions Program Counter (PC) 0 i int *prt_i = NULL ptr_i &i
41
Ok… now pointers? Memory./a.out So, if I do… ptr_i = &i; instructions Program Counter (PC) 0 i prt_i = &i; ptr_i
42
Ok… now pointers? Memory./a.out Now… what exactly is the arrow???? instructions Program Counter (PC) 0 i prt_i = &i; ptr_i
43
Ok… now pointers? Memory./a.out Now… what exactly is the arrow???? The arrow is the value of the pointer. printf("%p\n", prt_i); instructions Program Counter (PC) 0 i prt_i = &i; ptr_i
44
Ok… now pointers? Memory./a.out If I take * and apply it to ptr_i *ptr_i (in any context) instructions Program Counter (PC) 0 i prt_i = &i; ptr_i
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.