Professional Documents
Culture Documents
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?
score = 100;
grade = 'A';
stu_ID;
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;
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;
a
int a = 10;
10
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
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
15
16
n = 75.99;
n
75.99
0x12ff78
17
n
75.99
0x12ff78
ptr
0x12ff74
18
n
75.99
0x12ff78
ptr
ptr
= &n;
0x12ff78
0x12ff74
19
n
75.99
0x12ff78
double n = 75.99;
double *ptr;
ptr
ptr = &n;
cout << n << " " << &n
0x12ff78
0x12ff74
20
0x12ff6c
n
75.99
0x12ff78
ptr
0x12ff78
0x12ff74
21
0x12ff6c
n
75.99
0x12ff78
ptr
0x12ff6c
0x12ff74
22
= &n;
<< n << " " << &n
= &s;
= 2 * num;
n
75.99
0x12ff78
151.98
0x12ff6c
ptr
0x12ff6c
0x12ff74
23
= &n;
<< n << " " << &n
= &s;
= 2 * num;
<< s << " " << &s
n
75.99
0x12ff78
151.98
0x12ff6c
ptr
0x12ff6c
0x12ff74
24
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
28
29
30
31
32