12. Pointers

Pointers
COP3275 – PROGRAMMING USING C
DIEGO J. RIVERA-GUTIERREZ
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?
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????
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?
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
Cristina
Laura
arrayOfStudents[50]
Michael
…
[Empty]
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
Cristina
Juliana
Michael
arrayOfStudents[50]
What if Cristina decides to drop the class? How do we handle that in code?
…
[Empty]
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
0
1
Cristina
Juliana
2
3
arrayOfStudents[50]
Michael
4
What if Cristina decides to drop the class? How do we handle that in code?
…
[Empty]
49
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
0
1
[Empty]
Juliana
2
3
arrayOfStudents[50]
Michael
…
4
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.
[Empty]
49
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
0
1
[Empty]
Juliana
Michael
2
3
arrayOfStudents[50]
Option 2: We can move the last student (Michael) to Cristina’s space….
4
…
[Empty]
49
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
0
1
Michael
Juliana
[Empty]
2
3
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.
4
…
[Empty]
49
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
0
1
[Empty]
Juliana
2
3
arrayOfStudents[50]
Option 3: we move everyone just one slot.
Michael
4
…
[Empty]
49
Array of students
struct student arrayOfStudents[50];
Ana
Andrew
0
1
Juliana
Michael
2
3
arrayOfStudents[50]
[Empty]
4
…
[Empty]
49
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…
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).
Lists
• We will definitely implement a List next week. But essentially they look
like this:
Ana
Andrew
Cristina
Juliana
What if Cristina decides to drop the class? How do we handle that in code?
Michael
Lists
• We will definitely implement a List next week. But essentially they look
like this:
Ana
Andrew
Cristina
Juliana
What if Cristina decides to drop the class? How do we handle that in code?
Michael
Lists
• We will definitely implement a List next week. But essentially they look
like this:
Ana
Andrew
Cristina
Juliana
What if Cristina decides to drop the class? How do we handle that in code?
Michael
Lists
• We will definitely implement a List next week. But essentially they look
like this:
Ana
Andrew
Cristina
Juliana
What if Cristina decides to drop the class? How do we handle that in code?
Michael
Lists
• We will definitely implement a List next week. But essentially they look
like this:
Ana
Andrew
Cristina
Juliana
What if Cristina decides to drop the class? How do we handle that in code?
Michael
Lists
• We will definitely implement a List next week. But essentially they look
like this:
Ana
Andrew
Juliana
What if Cristina decides to drop the class? How do we handle that in code?
Michael
Lists
• So lists are cool…
Ana
Andrew
Cristina
Juliana
Michael
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
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)
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)
Andrew
Michael
Cristina
Ana
Juliana
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, …
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.
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.
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
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(<params>);
}else if(input == 'B‘) {
B(<params>);
}else if(input == 'C') {
C(<params>);
}
A
B
C
All of them are functions (pieces of code)
and they do similar functionality
Pointers to functions
• A pointer can point to anything in memory.
• That includes: code!
if(input == 'A' ) {
A(<params>);
}else if(input == 'B‘) {
B(<params>);
}else if(input == 'C') {
C(<params>);
}
A
B
C
D
• 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.
Pointers to functions
• A pointer can point to anything in memory.
• That includes: code!
A
if(input == 'A' ) {
B
A(<params>);
}else if(input == 'B‘) {
B(<params>);
C
}else if(input == 'C') {
C(<params>);
D
}else if(input == 'D'){
D(<params>);
}
That’s a bit annoying. Having to change the code… What if the person
That wrote D doesn’t have the original code?
I’ll talk about the specific type
of ptr next week. For now just
believe me: it is possible to this
with pointers
Pointers to functions
• A pointer can point to anything in memory.
• That includes: code!
A
• Let’s say we have a “magic” pointer ptr.
B
We could have this code:
if(ptr != NULL) {
ptr(<params>);
}
ptr
C
D
I’ll talk about the specific type
of ptr next week. For now just
believe me: it is possible to this
with pointers
Pointers to functions
• A pointer can point to anything in memory.
• That includes: code!
A
• Let’s say we have a “magic” pointer ptr.
B
We could have this code:
if(ptr != NULL) {
ptr(<params>);
}
ptr
C
D
I’ll talk about the specific type
of ptr next week. For now just
believe me: it is possible to this
with pointers
Pointers to functions
• A pointer can point to anything in memory.
• That includes: code!
A
• Let’s say we have a “magic” pointer ptr.
B
We could have this code:
if(ptr != NULL) {
ptr(<params>);
}
ptr
C
D
I’ll talk about the specific type
of ptr next week. For now just
believe me: it is possible to this
with pointers
Pointers to functions
• A pointer can point to anything in memory.
• That includes: code!
A
• Let’s say we have a “magic” pointer ptr.
B
We could have this code:
if(ptr != NULL) {
ptr(<params>);
}
ptr
This sort of idea is VERY common in optimization software.
Matlab uses it quite a bit.
C
D
E
…
Z
Let’s go back to the simple stuff….
Program Counter (PC)
./a.out
instructions
Memory
When we execute (e.g. %./a.out)
As execution continues, the PC points to
the next instruction to execute.
Let’s go back to the simple stuff….
./a.out
Program Counter (PC)
instructions
Memory
Let’s go back to the simple stuff….
./a.out
Program Counter (PC)
int i = 0;
0
i
instructions
Memory
When we reach variable definitions like
this one:
int i = 0;
When we reach pointer definitions like
this one:
int *prt_i = 0;
Ok… now pointers?
./a.out
Program Counter (PC)
int *prt_i = 0
0
i
instructions
ptr_i
Memory
When we reach pointer definitions like
this one:
int *prt_i = NULL;
Ok… now pointers?
./a.out
Program Counter (PC)
int *prt_i = NULL
0
i
instructions
ptr_i
Memory
When we use & and apply it to i
&i (in any context….)
Ok… now pointers?
./a.out
Program Counter (PC)
int *prt_i = NULL
&i
0
i
instructions
ptr_i
Memory
So, if I do…
ptr_i = &i;
Ok… now pointers?
./a.out
Program Counter (PC)
prt_i = &i;
instructions
0
i
ptr_i
Memory
Now… what exactly is the arrow????
Ok… now pointers?
./a.out
Program Counter (PC)
prt_i = &i;
instructions
0
i
ptr_i
Memory
Now… what exactly is the arrow????
The arrow is the value of the
pointer.
printf("%p\n", prt_i);
Ok… now pointers?
./a.out
Program Counter (PC)
prt_i = &i;
instructions
0
i
ptr_i
Memory
If I take * and apply it to ptr_i
*ptr_i (in any context)
Ok… now pointers?
./a.out
Program Counter (PC)
prt_i = &i;
instructions
0
i
ptr_i
Memory