1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

23
1 Pointers Revisited Linda M c Iver
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    212
  • download

    0

Transcript of 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

Page 1: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

1

Pointers Revisited

Linda McIver

Page 2: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

2

Pointer Examplesint i = 0;

0x2000 0x2001 0x2002 0x2003

Page 3: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

3

Pointer Examples

0

int i = 0;

i

0x2000 0x2001 0x2002 0x2003

Page 4: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

4

Pointer Examples

0

int i = 0;

int* intPtr = NULL;

i

0x2000 0x2001 0x2002 0x2003

int i = 0;

Page 5: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

5

Pointer Examples

0 NULL

int i = 0;

int* intPtr = NULL;

i intPtr

0x2000 0x2001 0x2002 0x2003

int i = 0;

Page 6: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

6

Pointer Examples

0 NULL

int i = 0;

int* intPtr = NULL;

intPtr = &i;

i intPtr

0x2000 0x2001 0x2002 0x2003

Page 7: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

7

Pointer Examples

0 0x2000

int i = 0;

int* intPtr = NULL;

intPtr = &i;

i intPtr

0x2000 0x2001 0x2002 0x2003

Page 8: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

8

Pointer Examples

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

What do these expressions evaluate to?

&i

intPtr

*intPtr

i

&intPtr

Page 9: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

9

Pointer Examples

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

What do these expressions evaluate to?

&i 0x2000

intPtr

*intPtr

i

&intPtr

Page 10: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

10

Pointer Examples

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

What do these expressions evaluate to?

&i 0x2000

intPtr 0x2000

*intPtr

i

&intPtr

Page 11: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

11

Pointer Examples

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

What do these expressions evaluate to?

&i 0x2000

intPtr 0x2000

*intPtr 0

i

&intPtr

Page 12: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

12

Pointer Examples

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

What do these expressions evaluate to?

&i 0x2000

intPtr 0x2000

*intPtr 0

i 0

&intPtr

Page 13: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

13

Pointer Examples

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

What do these expressions evaluate to?

&i 0x2000

intPtr 0x2000

*intPtr 0

i 0

&intPtr 0x2001

Page 14: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

14

Pointer Examples

&i == intPtr == 0x2000

i == *intPtr == 0

int i = 0;

int* intPtr = NULL;

intPtr = &i;

0 0x2000

i intPtr

0x2000 0x2001 0x2002 0x2003

Page 15: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

15

Pointer Examples

'b'

char c = 'b';

c

0x2000 0x2001 0x2002 0x2003

Page 16: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

16

Pointer Examples

'b' NULL

char c = 'b';

char* charPtr = NULL;

c charPtr

0x2000 0x2001 0x2002 0x2003

Page 17: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

17

Pointer Examples

'b' NULL

char c = 'b';

char* charPtr = NULL;

c charPtr

0x2000 0x2001 0x2002 0x2003

What does *charPtr

evaluate to now?

Page 18: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

18

Pointer Examples

'b' NULL 'L'

char c = 'b';

char* charPtr = NULL;

char initial = 'L';

c charPtr initial

0x2000 0x2001 0x2002 0x2003

Page 19: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

19

Pointer Examples

'b' 0x2000 'L'

char c = 'b';

char* charPtr = NULL;

char initial = 'L';

charPtr = &c;

c charPtr initial

0x2000 0x2001 0x2002 0x2003

Page 20: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

20

Pointer Examples

'b' 0x2000 'L'

char c = 'b';

char* charPtr = NULL;

char initial = 'L';

charPtr = &c;

c charPtr initial

0x2000 0x2001 0x2002 0x2003

What does *charPtr

evaluate to now?

Page 21: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

21

Pointer Examples

'b' 0x2002 'L'

char c = 'b';

char* charPtr = NULL;

char initial = 'L';

charPtr = &c;

charPtr = &initial;

c charPtr initial

0x2000 0x2001 0x2002 0x2003

Page 22: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

22

Pointer Examples

'b' 0x2002 'L'

char c = 'b';

char* charPtr = NULL;

char initial = 'L';

charPtr = &c;

charPtr = &initial;

c charPtr initial

0x2000 0x2001 0x2002 0x2003

What does *charPtr

evaluate to now?

Page 23: 1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.

23

Pointer Summary

• A pointer holds an address• & gives you the address of a variable• *somePtr is a de-reference, which gives

you the value of the variable pointed to by somePtr

• Like variables, pointers have a particular type