You are on page 1of 32

Introduction to Pointers

Examples:
1. Address of variables of different types memory map
2. Address of a variable code
3. Pointer variable
4. The address (&) and indirection (*) operators
5. Dereferencing an uninitialized pointer
6. Pointer variable
7. Pointer a derived data type
8. Why pointers?
9. Who is the inventor of the pointer variable concept?

1. Address of variables of different types memory map


int
char
int

score = 100;
grade = 'A';
stu_ID;

Memory: a sequence of bytes; each byte has its own address


10000101 01000001 11010010 00100011 11010010 11011010 011001011 00110101 01000001 00000000 00000000 00000000 01100100
19
20
21
22
23
24
25
26
27
28
29
30
31

Type
int
char
int

Name__
score
grade
stu_ID

Address_
0x28ff1c
0x28ff1b
0x28ff14

Value_____
100
A
8406144 // junk
1c = 1*16 + 12 = 28
1b = 1*16 + 11 = 27
14 = 1*16 + 4 = 20
2

int
char
int

score = 100;
grade = 'A';
stu_ID;

Memory: a sequence of bytes; each byte has its own address


stu_ID

grade

score

10000101 01000001 11010010 00100011 11010010 11011010 011001011 00110101 01000001 00000000 00000000 00000000 01100100
19
20
21
22
23
24
25
26
27
28
29
30
31

Type
Name__
Address_
Value_____
int
score
0x28ff1c
100
char
grade
0x28ff1b
A
int
stu_ID
0x28ff14
8406144 // junk
The address of a variable is the address of its first byte.
1c = 1*16 + 12 = 28
1b = 1*16 + 11 = 27
14 = 1*16 + 4 = 20
3

2. Address of a variable

int a = 10;

cout << a << endl;


cout << &a << endl;

a
int a = 10;

10
10
0x28ff1c

cout << a << endl;


cout << &a << endl;
Output:
10
0x28ff1c

3. Pointer Variable
a
int a = 10;
int *ptr;
ptr = &a;
cout
cout
cout
cout

<<
<<
<<
<<

a << endl;
ptr << endl;
&ptr << endl;
*ptr << endl;

10
10
0x28ff1c

ptr
0x28ff1c
0x28ff1c
0x28ff18

a
10
10

int a = 10;
int *ptr;

0x28ff1c

ptr = &a;
cout
cout
cout
cout

<<
<<
<<
<<

a << endl;
ptr << endl;
&ptr << endl;
*ptr << endl;

ptr
10
0x28ff1c
0x28ff18
10

0x28ff1c
0x28ff1c
0x28ff18

4. The address (&) and indirection (*) operators

b
25
25

int a = 10, b;
int *ptr;
ptr = &a;

ptr
&a
&a

cin >> a;
b = a + 1;
a++;
A pointer may be used instead of a variable's
name to access data.
Rewrite the last three statements using ptr.
8

b
25
25

int a = 10, b;
int *ptr;
ptr = &a;
cin >> a;
b = a + 1;
a++;

ptr
&a
&a
cin >> *ptr; // 25

b
25
25

int a = 10, b;
int *ptr;
ptr = &a;
cin >> a;
b = a + 1;
a++;

ptr
&a
&a
cin >> *ptr;
b = *ptr + 1; // 26

10

b
25
25

int a = 10, b;
int *ptr;
ptr = &a;
cin >> a;
b = a + 1;
a++;

ptr
&a
&a
cin >> *ptr;
b = *ptr + 1; // 26
*ptr++; // ???

11

b
25
25

int a = 10, b;
int *ptr;
ptr = &a;
cin >> a;
b = a + 1;
a++;

ptr
&a
&a
cin >> *ptr;
b = *ptr + 1; // 26
(*ptr)++;
// must use ( )

12

5. What is wrong?

double pi = 3.14;
double *ptr;
cout
cout
cout
cout

<< pi
<< &ptr
<< ptr
<< *ptr

<<
<<
<<
<<

endl;
endl;
endl;
endl;

pi
3.14
3.14

ptr

13

5. What is wrong?
Never dereference an uninitialized pointer!
double pi = 3.14;
double *ptr;
cout << pi
<< endl; // OK
cout << &ptr << endl; // OK
cout << ptr << endl; // OK
cout << *ptr << endl; // !
// never dereference an uninitialized pointer!

pi
3.14
3.14

ptr

Output
3.14
0x28ff14
0x784480
6.79231e+199

14

6. What is a pointer variable?

15

A pointer variable is a variable that can store an


address.

16

A pointer variable is a variable that can store an


address.
double

n = 75.99;

n
75.99
0x12ff78

17

A pointer variable is a variable that can store an


address.
double n = 75.99;
double *ptr;

n
75.99
0x12ff78

ptr
0x12ff74

18

A pointer variable is a variable that can store an


address.
double n = 75.99;
double *ptr;

n
75.99
0x12ff78

ptr
ptr

= &n;

0x12ff78
0x12ff74

19

A pointer variable is a variable that can store an


address.

n
75.99
0x12ff78

double n = 75.99;
double *ptr;

ptr
ptr = &n;
cout << n << " " << &n

0x12ff78

<< " " << ptr;

0x12ff74

20

A pointer variable is a variable that can store an


address.
s
double n = 75.99;
double *ptr;
double s;
ptr = &n;
cout << n << " " << &n

0x12ff6c

n
75.99
0x12ff78

ptr
0x12ff78

<< " " << ptr;

0x12ff74

21

A pointer variable is a variable that can store an


address.
s
double n = 75.99;
double *ptr;
double s;
ptr = &n;
cout << n << " " << &n
ptr = &s;

0x12ff6c

n
75.99
0x12ff78

ptr
0x12ff6c

<< " " << ptr;

0x12ff74

22

A pointer variable is a variable that can store an


address.
s
double n = 75.99;
double *ptr;
double s;
ptr
cout
ptr
*ptr

= &n;
<< n << " " << &n
= &s;
= 2 * num;

n
75.99
0x12ff78

151.98
0x12ff6c

ptr
0x12ff6c

<< " " << ptr;

0x12ff74

23

A pointer variable is a variable that can store an


address.
s
double n = 75.99;
double *ptr;
double s;
ptr
cout
ptr
*ptr
cout

= &n;
<< n << " " << &n
= &s;
= 2 * num;
<< s << " " << &s

n
75.99
0x12ff78

151.98
0x12ff6c

ptr
0x12ff6c

<< " " << ptr;

0x12ff74

<< " " << ptr;

24

7. A pointer type is a derived data type.


True/False

double a = 75.99;
double *p = &a;
int
int

b = 25;
*q = &b;

char
char

c = 'A';
*r = &c;

25

True/False
A pointer type is a derived data type.
A pointer type is a data type build from one of
the existing data types.
double a = 75.99;
double *p = &a;
int
int

b = 25;
*q = &b;

char
char

c = 'A';
*r = &c;
26

8. Why pointers?

27

Provide efficient techniques for manipulating


data in arrays.

28

Provide efficient techniques for manipulating


data in arrays.

They are the basis for dynamic allocation of


memory.

29

Provide efficient techniques for manipulating


data in arrays.

They are the basis for dynamic allocation of


memory.

They could be used in functions for reference


parameters.

30

9*. Who is the inventor of the pointer variable concept?

31

Harold W. Lawson "is the inventor


of the pointer variable concept
introduced into PL/I (1964) and later
into other widely utilized
programming languages"
Harold W. (Bud) Lawson was named one of three IEEE
Computer Society
Computer Pioneer Award winners for 2000, for inventing
the pointer variable and introducing this concept into PL/I,
thus providing for the first time the capability to flexibly treat
linked lists in a general-purpose high-level language.

32

You might also like