1 cs 201 introduction to pointers debzani deb. 2 incoming exam next monday (5th march), we will have...

29
1 CS 201 Introduction to Pointers Debzani Deb

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

1

CS 201Introduction to Pointers

Debzani Deb

Page 2: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

2

Incoming Exam• Next Monday (5th March), we will have Exam #1.• Closed book.• Sit with an empty space in either side of you.• Calculators that have text-allowing memory is not

permitted.• Covers Chapter 1-6 (Lecture Materials, Readings, Quiz,

Linux basics, Makefile, Eclipse, Pointer & Labs).• You should be able to answer correctly 80-85% of the

exam, if you get hold of the above materials. The rest 15-20% will be challenging.

• Email me/TA any question you have or see us at our office times. Don’t just wait for the last minute.

Page 3: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

3

What is a Pointer?• A pointer is a variable which points to a place in

computer’s memory. It contains memory address as its value ( POINTER =

ADDRESS) Pointer variable: a variable dedicated to hold memory

addresses.

• A variable name directly references a value (i.e. content of a memory cell).

• A pointer indirectly references a value. Referencing a value through a pointer is called indirection.

x int35

06510

p int *0658206571

06582contentAddress

Page 4: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

4

Pointers and Addresses Addr1

Addr2

Addr3

Addr4

Addr5

Addr6

.

.

.

Addr11

.

.

.

Addr15

Content1

Content2

Content11

Content15

• Memory can be conceptualized as a linear sequence of addressable locations.

• Variable reference the content of a location.

• Pointers have a value of the address of a location.

Page 5: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

5

Size of Variable Types

• Everything in a computer program must fit into high speed memory.

• High speed memory is simply a string of bytes. Lots and Lots and Lots of bytes, but just that…bytes.

• Some number of bytes are allocated every time you define a variable.

Page 6: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

6

High Speed Memory (RAM)

1073741823

01

2345

1Gb of RAM

Page 7: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

7

So What Is The Size Of Different Types?

printf(“sizeof(int) = %d\n”, sizeof(int));

If you ran a short program with a bunch of thesestatements.

• sizeof(int) = 4 sizeof(long) = 4

• sizeof(long int) = 4 sizeof(long long int) = 8

• sizeof(char) = 1 sizeof(float) = 4

• sizeof(double) = 8 sizeof(long double) = 12

Page 8: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

8

So What Is The Size Of Different Types?

• sizeof(int) = 4 sizeof(long) = 4

• sizeof(long int) = 4 (long long illegal on WinXp C++.Net)

• sizeof(char) = 1 sizeof(float) = 4

• sizeof(double) = 8 sizeof(long double) = 8

• Now, on to pointers!

Depends on hardware, brand of OS and compiler.

Here is Visual Studio .Net on Windows Xp Professional. (Differences are marked in red.)

Page 9: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

9

Again What is Pointer?

• A variable containing the address of another variable. (Hence it “points” to the other location).

• There are “things” and “pointers to things”

3thingthing_ptr

Reference Explanation Value

thing Direct value of the variable thing 3

thing_ptr Direct value of variable thing_ptr Pointer to location containing 3

*thing_ptr Indirect value of thing_ptr 3

Page 10: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

10

Structure Of A Pointer

Symbol Table

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

int x=2,y=4,z=6;memory

Address shown in hex.

name address

Page 11: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

11

Structure Of A Pointer

Symbol Table

X

Y

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C Z

int x=2,y=4,z=6;memory

name address

Page 12: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

12

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x=2,y=4,z=6;2

memory

4

6

name address

Note, I have shown the value of the variable in memory, not the actual representation!

Page 13: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

13

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x=2,y=4,z=6;int * px; px = &x;

memory

2

6

4

name address

Page 14: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

14

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

px

memory

px

0F45AB24

2

6

4

0F45AB14

name address

int x=2,y=4,z=6;int * px; px = &x;

Page 15: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

15

So, what is the size of a pointer?

• sizeof(int *) = 4• sizeof(long long int *) = 4• sizeof(float *) = 4• sizeof(double *) = 4• sizeof (char * ) = 4• Pointers to different types are all the same size.• Once again, their size may be dependent on your

hardware, OS, or even compiler.• Now, back to my diagram.

Page 16: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

16

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

px

memory

px

0F45AB24

2

6

4

0F45AB14

name address

int x=2,y=4,z=6;int * px; px = &x;

Page 17: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

17

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x);

name address

int x=2,y=4,z=6;int * px; px = &x;

Page 18: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

18

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x); 2

name address

int x=2,y=4,z=6;int * px; px = &x;

Page 19: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

19

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x); 2

printf(“%d\n”,*px);

int x=2,y=4,z=6;int * px;px = &x;

Page 20: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

20

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x); 2

printf(“%d\n”,*px);2

Page 21: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

21

Pointer Declaration

int * p; // pointer is declaredp is a pointer pointing at an variable of type int.• * tells the compiler that the variable named p is to be

a pointer.• The name of the pointer variable is p (not *p).• The size of the memory addressed by a pointer

depends on the type of the object mentioned in declaration.int * p; // p points at 4 consecutive bytesdouble * p; // p points at 8 consecutive bytes

Page 22: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

22

Operators Related To Pointers

• & address-of printf(“%p\n”,&variable);

• * dereference printf(“%d\n”, * pointer_variable)

Page 23: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

23

Address of Operator (&)

int x = 3; // Variable defined

int * p; // Pointer declared

p = &x; // Pointer p is set up to point at // the variable x.

Address of variable x is assigned to pointer p.

• & is an unary address operator which returns the memory address of variable.

• &x is read as “address of x”

Page 24: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

24

About Asterisk (*)

• In declaration * tells the compiler that a new variable is to be a

pointer (read as “pointer to”)• In Regular use

It provides the content of the memory location specified by a pointer. It mean “follow the pointer”.

It can also stand on the left side of an assignment.

int * p

x = * p

* p = 10

Page 25: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

25

Typical Uses Of Operators

• int thing; /* declare an integer (a thing) */• thing = 4;• int * thing_ptr; /* declare a pointer to an integer */

Don’t need _ptr, just helps readability. At this moment, it is pointing to a random values since it

has not been initialized yet. Such garbage can be harmful.

• thing_ptr = NULL; thing_ptr is a null pointer means “pointing to nothing”.

• thing_ptr = &thing; /* store address in pointer, points to thing */

Page 26: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

26

Cont.

• *thing_ptr = 5; /* set thing to 5 */ *thing_ptr is a thing. thing_ptr is a pointer.

• Of course, thing_ptr could be set to point to other variables besides thing. thing_ptr = &something_else;

Page 27: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

27

Illegal and Strange Uses

• *thing is illegal. thing is not a pointer variable.

• &thing_ptr is legal, but strange. “a pointer to a pointer”

Page 28: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

28

Pointing to the Same Thing

int something;int *first_ptr; /* one pointer */int *second_ptr; /* another pointer */something=1;first_ptr = &something;second_ptr = first_ptr;

something=1;*first_ptr=1;*second_ptr=1;

All identical results

(Only one integer)

Page 29: 1 CS 201 Introduction to Pointers Debzani Deb. 2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in

29

Again uses of & and *

p = &x;// p points at x now.

y = * p;// y has the value pointed out by the pointer p.

* p= 13;// 13 was inserted to the

// place pointed out by p.

x int35

06510p int *

0651006571

06510

y int35

06587

x int13

06510p int *

0651006571

06510