/
Pointers Pointers

Pointers - PowerPoint Presentation

kittie-lecroy
kittie-lecroy . @kittie-lecroy
Follow
437 views
Uploaded On 2016-06-19

Pointers - PPT Presentation

COP3275 Programming using c Diego J RiveraGutierrez 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 ID: 368491

pointers ptr memory code ptr pointers code memory cristina michael ana andrew pointer juliana arrayofstudents empty functions params lists input student struct

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Pointers" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Pointers

COP3275 – Programming using c

Diego J. Rivera-GutierrezSlide2

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?Slide3

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???? Slide4

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?Slide5

Array of students

Andrew

Michael

Cristina

Ana

Laura

[Empty]

arrayOfStudents

[50]

struct

student

arrayOfStudents

[50];

…Slide6

Array of students

Andrew

Michael

Cristina

Ana

Juliana

[Empty]

arrayOfStudents

[50]

struct

student

arrayOfStudents

[50];

What if Cristina decides to drop the class? How do we handle that in code?Slide7

Array of students

Andrew

Michael

Cristina

Ana

Juliana

[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

49Slide8

Array of students

Andrew

Michael

[Empty]

Ana

Juliana

[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:

all our functions on the list, would have to account for possible empty spaces before

the end of the array.

We always have to visit the whole array instead of just the active students.

0

1

2

3

4

49Slide9

Array of students

Andrew

Michael

[Empty]

Ana

Juliana

[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

49Slide10

Array of students

Andrew

[Empty]

Michael

Ana

Juliana

[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

49Slide11

Array of students

Andrew

Michael

[Empty]

Ana

Juliana

[Empty]

arrayOfStudents

[50]

struct

student

arrayOfStudents

[50];

Option 3:

we move everyone just one slot.

0

1

2

3

4

49Slide12

Array of students

Andrew

[Empty]

Juliana

Ana

Michael

[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

49Slide13

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

Lists

We will definitely implement a List next week. But essentially they look like this:

Ana

Andrew

Cristina

Juliana

Michael

What if Cristina decides to drop the class? How do we handle that in code?Slide15

Lists

We will definitely implement a List next week. But essentially they look like this:

Ana

Andrew

Cristina

Juliana

Michael

What if Cristina decides to drop the class? How do we handle that in code?Slide16

Lists

We will definitely implement a List next week. But essentially they look like this:

Ana

Andrew

Cristina

Juliana

Michael

What if Cristina decides to drop the class? How do we handle that in code?Slide17

Lists

We will definitely implement a List next week. But essentially they look like this:

Ana

Andrew

Cristina

Juliana

Michael

What if Cristina decides to drop the class? How do we handle that in code?Slide18

Lists

We will definitely implement a List next week. But essentially they look like this:

Ana

Andrew

Cristina

Juliana

Michael

What if Cristina decides to drop the class? How do we handle that in code?Slide19

Lists

We will definitely implement a List next week. But essentially they look like this:

Ana

Andrew

Juliana

Michael

What if Cristina decides to drop the class? How do we handle that in code?Slide20

Lists

So lists are cool…

Ana

Andrew

Cristina

Juliana

MichaelSlide21

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

MichaelSlide22

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) Slide23

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

MichaelSlide24

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

, …Slide25

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

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

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 functionalitySlide28

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 functionalitySlide29

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>);

}

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

DSlide30

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>);

}

else if(input == 'D'){

D(<

params

>);

}

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?Slide31

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(<params

>);}

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 pointersSlide32

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(<params

>);}

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 pointersSlide33

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(<params

>);}

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 pointersSlide34

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(<params

>);}

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

Let’s go back to the simple stuff….

Memory

./

a.out

When we execute (e.g. %./

a.out

)

instructions

Program Counter (PC)Slide36

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)Slide37

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;Slide38

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_iSlide39

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_iSlide40

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

&

iSlide41

Ok… now pointers?

Memory

./

a.out

So, if I do…

ptr_i

= &

i

;

instructions

Program Counter (PC)

0

i

prt_i

=

&

i

;

ptr_iSlide42

Ok… now pointers?

Memory

./

a.out

Now… what exactly is the arrow????

instructions

Program Counter (PC)

0

i

prt_i

=

&

i

;

ptr_iSlide43

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_iSlide44

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_iSlide45