Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
©
Christian Jacob
Chapter Overview
Chapter 12
Pointers(Part 1)
12.1 Pointer Basics
12.1.1 What Are Pointers?
12.1.2 Pointer Operators
12.2 Working with Pointers
12.2.1 Assigning Values Through Pointers
12.2.2 Pointer Expressions
12.2.3 Pointer Comparisons
12.3 Pointers and Function Parameters
12.4 References
Page 2 Chapter 11: Pointers © Christian Jacob
Prev Next Last
dress. variable.
n in C++ is:
riable
riable locatedits operand.
First Back TOC
12.1 Pointer Basics
12.1.1 What Are Pointers?
A pointer is a variable that contains a memory ad
Very often this address is the location of another
The general form of a pointer variable declaratio
type
*
variable-name
;
•
type
the pointer´s base type.It must be a valid C++ type.
•
variable-name
the name of the pointer va
•
*
the “at address” operatorReturns the
value
of the vaat the address specified by
Page 3 Chapter 11: Pointers © Christian Jacob
Prev Next Last
er to integer
er to float
ar or string
pointer
First Back TOC
Examples of pointers:
int *int_pointer; // point
float *float_pointer; // point
char *str; // pointer to a ch
int **ptrptr; // pointer to a
Page 4
Chapter 11: Pointers
©
Christian Jacob
Prev Next Last
th pointers:
urns the memory
ator
urns the value of address specified by
First Back TOC
12.1.2 Pointer Operators
There are two special operators that are used wi
• & : “
address of …
” operator
A unary operator which ret
address
of its operand.
•
*
:“
value at address …
” oper
A unary operator which retthe variable located at the its operand.
Page 5 Chapter 11: Pointers © Christian Jacob
Prev Next Last
00 bal_ptr
- balance
- value
. . .
. . .
First Back TOC
Example:
int balance;int *balptr;
balptr = &balance; 112
100
130
Page 6 Chapter 11: Pointers © Christian Jacob
Prev Next Last
100 bal_ptr
3200 balance
3200 value
. . .
. . .
Step 3:
First Back TOC
Example:
int balance, value;int *balptr;
balance = 3200; // Step 1balptr = &balance; // Step 2
value = *balptr; // Step 3
-12
bal_ptr
3200
100
balance
-
130
value
. . .
. . .
Step 1:
100
12
bal_ptr
3200
100
balance
-
130
value
. . .
. . .
Step 2:
12
100
130
Page 7 Chapter 11: Pointers © Christian Jacob
Prev Next Last
o
value
from the
r of bytes for any
s the type of data g to.
a type cast
First Back TOC
Importance of the Base Type
How does C++ know how many bytes to copy intaddress pointed to by balptr
?
How does the compiler know the proper numbeassignment using a pointer?
Answer: The
base type
of the pointer determinethat the
compiler assumes
the pointer is pointin
The following code fragment is incorrect:
int *int_ptr; double f;int_ptr = &f; // ERROR
Technically correct, but not recommended (using
operator):
int_ptr = (int *) &f;
Page 8 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ith pointers:
to assigno int*
s do?s print?
First Back TOC
Example for why to be careful about type casts w
void main(){
double x, y;int *ptr;
x = 123.23;
ptr
= (int *) &x;
// use cast // double* t
y = *
ptr
;
// What will thi
cout << y;
// What will thi
}
Page 9 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ent statements.
the location
lue 101.”
pointers, too.
e value by 1.”
First Back TOC Working with Pointers
12.2 Working with Pointers
12.2.1 Assigning Values Through Pointers
• Pointers can be used on the left side of assignm
The following code fragment assigns a value topointed to by the pointer.
int *ptr;*ptr = 101;
“At the location pointed to by
p
, assign the va
• Increment and decrement operations work on
(*ptr)++;
“At the location pointed to by
p
, increment th
Page 10 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ointers
:
*_int ptr
_int num
-
. . .
. . .
Step 1
First Back TOC Working with Pointers
Example program for assigning values through p
void main(){
int *ptr, num; // 1
ptr = #
// 2
*ptr = 100;
// 3
cout << num << ‘ ‘;
(*ptr)++;
// 4
cout << num << ‘ ‘;
(*ptr)*2;
// 5
cout << num << ‘\n’;}
12
50
Page 11 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ointers
:
50 ptr
_int num
-
. . .
. . .
Step 2
First Back TOC Working with Pointers
Example program for assigning values through p
void main(){
int *ptr, num; // 1
ptr = #
// 2
*ptr = 100;
// 3
cout << num << ‘ ‘;
(*ptr)++;
// 4
cout << num << ‘ ‘;
(*ptr)*2;
// 5
cout << num << ‘\n’;}
12
50
Page 12 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ointers
:
50 ptr
100 num
-
. . .
. . .
Step 3
First Back TOC Working with Pointers
Example program for assigning values through p
void main(){
int *ptr, num; // 1
ptr = #
// 2
*ptr = 100;
// 3
cout << num << ‘ ‘;
(*ptr)++;
// 4
cout << num << ‘ ‘;
(*ptr)*2;
// 5
cout << num << ‘\n’;}
12
50
Page 13 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ointers
:
50 ptr
101 num
-
. . .
. . .
Step 4
First Back TOC Working with Pointers
Example program for assigning values through p
void main(){
int *ptr, num; // 1
ptr = #
// 2
*ptr = 100;
// 3
cout << num << ‘ ‘;
(*ptr)++;
// 4
cout << num << ‘ ‘;
(*ptr)*2;
// 5
cout << num << ‘\n’;}
12
50
Page 14 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ointers
:
50 ptr
202 num
-
. . .
. . .
Step 5
First Back TOC Working with Pointers
Example program for assigning values through p
void main(){
int *ptr, num; // 1
ptr = #
// 2
*ptr = 100;
// 3
cout << num << ‘ ‘;
(*ptr)++;
// 4
cout << num << ‘ ‘;
(*ptr)*2;
// 5
cout << num << ‘\n’;}
12
50
Page 15 Chapter 11: Pointers © Christian Jacob
Prev Next Last
expressions.
inters:
First Back TOC Working with Pointers
12.2.2 Pointer Expressions
Pointers can be used in most C++ expressions.
Keep in mind to use parentheses around pointer
Pointer Arithmetic
Only four arithmetic operators can be used on po
•
++
•
--
•
+
•
-
Page 16 Chapter 11: Pointers © Christian Jacob
Prev Next Last
another. numbers to pointers:
byte 4byte 4
byte 4
First Back TOC Working with Pointers
Example: (assuming 32-bit integers)
int *p1; // assume: p1 == 2000
p1++;
• Integers can be added or subtracted from pointers:
• You can subtract pointers of the same type from oneYou can not add pointers! However, you can add
int
2000 byte 1 byte 2 byte 32004 byte 1 byte 2 byte 3
1996 byte 1 byte 2 byte 3
. . .
. . .
Page 17 Chapter 11: Pointers © Christian Jacob
Prev Next Last
t element of i
first element of f
First Back TOC Working with Pointers
void main(){
int i[10], *intPtr;double d[10], *doublePtr;int x;
intPtr = i; // i_ptr points to firsdoublePtr = d;
// f_ptr points to
for(x=0; x < 10; x++)cout << intPtr + x;cout << ‘ ‘;cout << doublePtr + x;cout << endl;
}
Page 18 Chapter 11: Pointers © Christian Jacob
Prev Next Last
First Back TOC Working with Pointers
Output of the example program:
The addresses of the array elements:
4 bytes int 8 bytes double
0xeffffd9c 0xeffffd480xeffffda0 0xeffffd500xeffffda4 0xeffffd580xeffffda8 0xeffffd600xeffffdac 0xeffffd680xeffffdb0 0xeffffd700xeffffdb4 0xeffffd780xeffffdb8 0xeffffd800xeffffdbc 0xeffffd880xeffffdc0 0xeffffd90
Page 19 Chapter 11: Pointers © Christian Jacob
Prev Next Last
e have to use the
doublePtr;
t element of i
first element of f
First Back TOC Working with Pointers
If we want to see the values at these addresses, w"value at ..." operator (*):
void main(){
int i[3]={1,2,3}, *intPtr;double d[3]={1.1,2.2,3.3}, *int x;
intPtr = i; // i_ptr points to firsdoublePtr = d;
// f_ptr points to
for(x=0; x < 3; x++)cout << *(intPtr + x);cout << ’ ’;cout << *(doublePtr + x);cout << endl;
}
Page 20 Chapter 11: Pointers © Christian Jacob
Prev Next Last
tors, such as:
;
First Back TOC Working with Pointers
12.2.3 Pointer Comparisons
Pointers may be compared using relational opera !=, ==, <
, and
>
.
void main(){
int num[10];int *
start
, *
end
;
start
= num;
end
= &num[9];
while(
start
!=
end
) {cout << “Enter a number: “cin >> *
start
;
start
++;} }
Page 21 Chapter 11: Pointers © Christian Jacob
Prev Next Last
c
tors, such as
!=
,
==
,
;
First Back TOC Working with Pointers
Pointer Comparisons (2): using pointer arithmeti
Pointers may be compared using relational opera<
, and
>
.
void main(){
int num[10];int *
start
, *
end
;
start
= num;
end
= &num[9];
while((
end
-
start)
> 0) {cout << “Enter a number: “cin >> *
start
;
start
++;} }
Page 22 Chapter 11: Pointers © Christian Jacob
Prev Next Last
ers
t &j);
a side effect
First Back TOC Pointers and Function Parameters
12.3 Pointers and Function Paramet
Back to Mine Sweeper:
void GetCoordinates(int &i, in
...
void main(){ ...
int i, j; // local variables
GetCoordinates(i, j);
// Manipulates coordinates as
...}
Page 23 Chapter 11: Pointers © Christian Jacob
Prev Next Last
, Boston, MA: WCB/
ill, Berkeley, CA,
First Back TOC References
12.4 References
• G. Blank and R. Barnes, The Universal MachineMcGraw-Hill, 1998. Chapter 9.
• H. Schildt, C++ from the Ground Up, McGraw-H1998. Chapter 6.