Title: I hope you:
1CS110 Lecture 17Pointers , , and Functions
Jack Tumblinjet_at_cs.northwestern.edu
- I hope you
- Are finished reading Chapter 9.
- Are 'halfway' done with PA-3 (Warning!
time-consuming!)
2 Referencing with Pointers
- The operator lets a pointer refer to ANY
variable of the same base type - int i2,j3 / variety of integers /
- int list4 1,6,4,8
- int pNow, p1, p2 / integer pointers /
- p1 i / p1 refers to i / pNow
list1 / pNow0 list1 / p2 1
(list2) / p2 refers to list3/ - printf(d, d, d\n,pNow0, p10, p20)
- Result
- gt 6, 2, 8
- gt
Note pointer names often begin or end with p
Note we can reference array elements!
3Pointer De-Referencing by p0
- Referencing point to an ordinary variable
var pNum (dist) - De-referencing find or set the value where we
point pNum03.2 - Looks a bit silly element 0 of a one-element
array
Result gt dist is 82.481 gt size dist-gt8 bytes,
pNum-gt4 bytesgt
int pNum /int ptr/double dist 82.481
pNum dist printf(dist is f\n,
pNum0) printf(size dist-gtd bytes,pNum-gt
d bytes\n, sizeof(dist), sizeof(pNum))
4DANGER! De-Referencing by p
- Referencing point to an ordinary variable
var pNum (dist) - De-referencing find/set the value where we
point pNum03.2 - Looks a bit silly element 0 of a one-element
array - Alternative use the dereferencing operator
var - ? !!DONT CONFUSE WITH DECLARATION!!! ?
int pNum /int ptr/double dist 82.481
pNum dist printf(dist is f or f or
f\n,dist,pNum0,pNum) printf(size
dist-gtd bytes,pNum-gt d bytes\n,
sizeof(dist), sizeof(pNum))
Result gt dist is 82.481 or 82.481 or 82.481 gt
size dist-gt8 bytes, pNum-gt4 bytesgt
5Reference, De-reference
- Referencing address-of operator ptr var
- Indirection I by array index var
ptr0 - Indirection II by operator var
ptr - CAREFUL
- Never confuse pointer declaration with
indirection operator. - Address-of, indirection operators CANCEL
does nothing. - Address-of, indirection operator precedence is
BELOW ,-, , -- - Know how to read (BUT NEVER USE!) ugly
expressions like these - ipDigit (pDigit) I --(pDigit)
int y2k4 2,0,0,2 int pDigit, i0
pDigit y2k i pDigit /what did we
do? y2k 2,0,0,2 i0 / printf(d,
d\n,i, pDigit)
6Pointers Why Bother?
- Why are pointers any better than an array index?
- Because
- One pointer can select from many arrays
- Pointers can point to (hold the address of)
much more than just arrays --also-- - Any array or array element
- Any variable (!)
- Any function (!!)
- point to other pointers(!!!)
- pointers can link together complex webs
of data - (later, with structures)
7Pointers Functions Example 0
void swapNO(int a, int b) /fcn.
prototype/ int main (void)int x10, y25
/ declare, define 2 integers /int px, py
/ declare 2 integer pointers / px x
/ px points to x.../ py y
printf(xd,yd\n,x,y) swapNO(x, y)
/TRY TO swap values x, y /
printf(xd,yd\n,x,y) return 0 void
swapNO(int a, int b)int tmp tmp a
a b b tmp
Result gt x10,y25gt x10,y25 WHY DOES THIS
FAIL?
the function parameters a,b (local variables in
the function) are int COPIED FROM the arguments
x,y
8Pointers Functions Example I
void swap1(int pa, int pb) /fcn.
prototype/ int main (void)int x10, y25
/ declare, define 2 integers /int px, py
/ declare 2 integer pointers / px x
/ px points to x.../ py y
printf(xd,yd\n,x,y) swap1(px, py)
/swap values POINTED TO BY px,py/
printf(xd,yd\n,x,y) return 0 void
swap1(int pa, int pb)int tmp tmp
pa0 pa0 pb0 pb0 tmp
Result gt x10,y25gt x25,y10
the function parameters (local variables in the
function) are int array names. De-reference
using array elements.
9Pointers Functions Example II
void swap2(int pa, int pb) /fcn.
prototype/ int main (void)int x10, y25
/ declare, define 2 integers /int px, py
/ declare 2 integer pointers / px x
/ px points to x.../ py y
printf(xd,yd\n,x,y) swap2(px, py)
/swap values POINTED TO BY px,py/
printf(xd,yd\n,x,y) return 0 void
swap2(int pa, int pb)int tmp tmp
pa0 pa0 pb0 pb0 tmp
Result gt x10,y25gt x25,y10
the function parameters (local variables in the
function) are int pointers. De-reference using
array elements.
10Pointers Functions Example III
void swap3(int pa, int pb) /fcn.
prototype/ int main (void)int x10, y25
/ declare, define 2 integers /int px, py
/ declare 2 integer pointers / px x
/ px points to x.../ py y
printf(xd,yd\n,x,y) swap3(px, py)
/swap values POINTED TO BY px,py/
printf(xd,yd\n,x,y) return 0 void
swap3(int pa, int pb)int tmp tmp
pa pa pb pb tmp
Result gt x10,y25gt x25,y10
the function parameters (local variables in the
function) are int pointers. De-reference using
indirection op.
11Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
912
916
920
924
12Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px ???
py ???
912
916
920
924
13Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px 900
py ???
912
916
920
924
14Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px 900
912
py 904
916
920
924
15Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px 900
COPY
COPY
912
py 904
pa 900
916
pb 904
920
924
16Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px 900
912
py 904
pa 900
916
pb 904
920
924
tmp???
17Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px 900
912
py 904
pa 900
916
pb 904
920
924
tmp 10
18Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 25
900
y 25
904
908
px 900
912
py 904
pa 900
916
pb 904
920
924
tmp 10
19Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 25
900
y 10
904
908
px 900
912
py 904
pa 900
916
pb 904
920
924
tmp 10
20Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 25
900
y 10
904
908
px 900
912
py 904
916
920
924
21Pointers and Functions
- Even though the swap3() function has no return
value, it has a useful side effect it
changes values at the given pointers - Actual arguments to swap3() are pointerspx and
py that hold x and y addresses. - Idea! Why not use address-of operator? No need
for separate pointer variables px, py instead,
just pass the addresses of x and y to swap3(x)
22Pointers Functions Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(x, y)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
Yes! Simpler, and it works.
y 25
904
908
912
916
920
924