![Page 1: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/1.jpg)
REFERENCES, POINTERSPASSING PARAMETERS TO FUNCTIONS
Problem Solving with Computers-I
a
![Page 2: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/2.jpg)
Announcements• H05 and H06 are released. (pdf versions available on website) • Please submit a pdf version of your answers to the assignment on gradescope
before the due date • Print, write by hand, scan, upload • Download, annotate, upload • Use Word (or some other text editor to write the answers only), convert to
pdf and upload.
![Page 3: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/3.jpg)
�3
p112
y3
100 112
Pointer Diagrams: Diagrams that show the relationship between pointers and pointees
Pointer: p Pointee: y
p points to y
![Page 4: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/4.jpg)
Tracing code involving pointers
Q: Which of the following pointer diagrams best represents the outcome of the above code?
�4
int *p;int x = 10;p = &x;*p = *p + 1;
A. 10x
B.x
C. Neither, the code is incorrect
11
p p
H
B Dgp
O
![Page 5: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/5.jpg)
Pointer assignment
Q: Which of the following pointer diagrams best represents the outcome of the above code?
�5
int *p1, *p2, x;p1 = &x;p2 = p1;
A.
xB.
x
C. Neither, the code is incorrect
p1
p2
p1 p2
fi p intm.psu
Ointoplasp3xipleadp2z pl
32 points topl
![Page 6: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/6.jpg)
▪ ar is like a pointer to the first element
▪ ar[0] is the same as *ar
▪ ar[2] is the same as *(ar+2)
ar
100 104 108 112 116
20 30 50 80 90
▪ Use pointers to pass arrays in functions▪ Use pointer arithmetic to access arrays more conveniently
Arrays and pointersI
arco art 2
AddThe size 82dat ints to ar
O I 2 3 Y
Pointerarithmetic
F Address ofelemental
index
2
O
![Page 7: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/7.jpg)
What is the output of the code?
A. Mark Jill
B. Mark Mark
C. Art Mark
D. Compiler error
E. Run-time error
char s1[] = "Mark"; char s2[] = "Jill"; for (int i = 0; i <= 4; i++) s2[i] = s1[i]; if (s1 == s2) s1 = "Art"; cout<<s1<<" "<<s2<<endl;
0 1001
sik
0 I 2 3 40 8000
52 fMT0 I 23 Y
Tcompiler eesorcus it bate
addressofarray cannot change thebase
are equal address ofthe away
![Page 8: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/8.jpg)
void swapValue(int x, int y){ int tmp = x; x = y; y = tmp; }int main() {
int a=30, b=40;
cout<<a<<" "<<b<<endl; swapValue( a, b); cout<<a<<" "<<b<<endl;
}
Pass by valueWhat is printed by this code? A.
30 40
30 40
B.
30 40
40 30
C. Something else
swap Value0 x TodX y are copies
Do bfLocal var
40 30 Fain
![Page 9: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/9.jpg)
g a
Void swap Value intern integD
int temp a x f t
x z y ney are
y twp PREFERENCES
int main C 3 nice names
Mf az 30 b 401 for other variak
swap ValinCa bl
y passby referene tmp
ba
main TobX y
notice the differencebetween
the memory diagramon
this pagecompared
to
the previous pagewhere
variables were passed byValley
![Page 10: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/10.jpg)
References in C++
int main() { int d = 5; int &e = d; }
A reference in C++ is an alias for another variable
!9
7
eisa dgg5 Daoreference Hd E int x
d 10 2 4e 20 Kjcourted
![Page 11: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/11.jpg)
References in C++
int main() { int d = 5; int &e = d; int f = 10; e = f; }
How does the diagram change with this code?
C. 10
10d:e:
10f:
A. B.5
10
D. Other or error
!10
d:e:f:
d:e:f:
could d
O
![Page 12: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/12.jpg)
Passing parameters by reference
void swapValue(int x, int y){ int tmp = x; x = y; y = tmp; }int main() {
int a=30, b=40;
swapValue( a, b);
cout<<a<<" "<<b<<endl;
} UO 30
![Page 13: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/13.jpg)
Passing parameters by address
void swapValue(int x, int y){ int tmp = x; x = y; y = tmp; }int main() {
int a=30, b=40;
swapValue( a, b);
cout<<a<<" "<<b<<endl;
}
p ya D D
Imp f bPo affb
o
Passbyaddress the address ofa b
are copied intopointersX and y
![Page 14: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/14.jpg)
Pointer Arithmetic▪ What if we have an array of large structs (objects)? ▪C++ takes care of it: In reality, ptr+1 doesn’t add 1 to the
memory address, but rather adds the size of the array element. ▪C++ knows the size of the thing a pointer points to – every
addition or subtraction moves that many bytes: 1 byte for a char, 4 bytes for an int, etc.
![Page 15: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/15.jpg)
Pointer Arithmetic
int *p;p = arr;p = p + 1;*p = *p + 1;
Draw the array ar after the above code is executed
int ar[]={20, 30, 50, 80, 90};
100 104 108
![Page 16: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/16.jpg)
Pointer Arithmetic
How many of the following are invalid?I. pointer + integer (ptr+1)II. integer + pointer (1+ptr)III. pointer + pointer (ptr + ptr)IV. pointer – integer (ptr – 1)V. integer – pointer (1 – ptr)VI. pointer – pointer (ptr – ptr)VII. compare pointer to pointer (ptr == ptr)VIII. compare pointer to integer (1 == ptr)IX. compare pointer to 0 (ptr == 0)X. compare pointer to NULL (ptr == NULL)
#invalid A: 1 B: 2 C: 3 D: 4 E: 5
int ar[]={20, 30, 50, 80, 90};
100 104 080 I 2 3 Y
pH pin I
x
![Page 17: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/17.jpg)
int a = 5; int &b = a; int *pt1 = &a;
What are three ways to change the value of ‘a’ to 42?
!16
Pointers and references: Draw the diagram for this code
![Page 18: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/18.jpg)
Which of the following is true after IncrementPtr(q)is called in the above code:
void IncrementPtr(int *p){ p++; }
50 60 70arr
qint arr[3] = {50, 60, 70}; int *q = arr; IncrementPtr(q);
A. ‘q’ points to the next element in the array with value 60 B. ‘q’ points to the first element in the array with value 50
![Page 19: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/19.jpg)
How should we implement IncrementPtr(),so that ‘q’ points to 60 when the following code executes? void IncrementPtr(int **p){ p++; }
50 60 70arr
qint arr[3] = {50, 60, 70}; int *q = arr; IncrementPtr(&q);
A. p = p + 1; B. &p = &p + 1; C. *p= *p + 1; D. p= &p+1;
![Page 20: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/20.jpg)
Pointer pitfalls• Dereferencing a pointer that does not point to anything results in
undefined behavior.
• On most occasions your program will crash
• Segmentation faults: Program crashes because code tried to access memory location that either doesn’t exist or you don’t have access to
![Page 21: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/21.jpg)
Two important facts about Pointers�20
1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct, another pointer, etc
2) After declaring a pointer: int *ptr; ptr doesn’t actually point to anything yet. We can either:
➢make it point to something that already exists, OR➢allocate room in memory for something new that it will point to
![Page 22: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/22.jpg)
Two important facts about Pointers!21
1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct, another pointer, etc
2) After declaring a pointer: int *ptr;
ptr doesn’t actually point to anything yet. We can either:
➢make it point to something that already exists, OR
➢allocate room in memory for something new that it will point to
➢Null check before dereferencing
![Page 23: REFERENCES, POINTERS PASSING PARAMETERS TO FUNCTIONS · Two important facts about Pointers!21 1) A pointer can only point to one type –(basic or derived ) such as int, char, a struct,](https://reader034.vdocuments.net/reader034/viewer/2022042521/5f5f10fbe483381f3d4e66b3/html5/thumbnails/23.jpg)
Next time• Structs • Arrays of structs