Download presentation
Presentation is loading. Please wait.
Published byDennis Atkinson Modified over 8 years ago
1
CPSC 221 Call by value, pointer and reference in C++ Page 1 Hassan Khosravi January – April 2015 CPSC 221 Basic Algorithms and Data Structures Call by value, pointer, and reference in C++ Stealing some of the examples from Alan
2
CPSC 221 Call by value, pointer and reference in C++ Page 2 Clicker Question What would be printed to the screen after running the following code? void swap1(int x, int y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap1(a,b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
3
CPSC 221 Call by value, pointer and reference in C++ Page 3 Call By Value What would be printed to the screen after running the following code? void swap1(int x, int y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap1(a,b); std::cout << a << "," << b; } main b=1 a=0 main b=1 a=0 swap1 t y=1 x=0 swap1 t y=1 x=0 200 240 500 560 600 A: 0,1 main b=1 a=0 main b=1 a=0 swap1 t=0 y=0 x=1 swap1 t=0 y=0 x=1
4
CPSC 221 Call by value, pointer and reference in C++ Page 4 x=0 x=1 t t t=0 Call By Value a=0 b=1 main() swap1(int x, int y) y=1 600 560 540 200 240 y=0 a = 0, b = 1 int t = x y = t x = y
5
CPSC 221 Call by value, pointer and reference in C++ Page 5 Clicker Question What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap2(a,b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
6
CPSC 221 Call by value, pointer and reference in C++ Page 6 Attempting to Use Pointers What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap2(a,b); std::cout << a << "," << b; } C: Does not compile
7
CPSC 221 Call by value, pointer and reference in C++ Page 7 Clicker Question What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap2(&a,&b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
8
CPSC 221 Call by value, pointer and reference in C++ Page 8 Attempting to Use Pointers 2 What would be printed to the screen after running the following code? void swap2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap2(&a,&b); std::cout << a << "," << b; } main b=1 a=0 main b=1 a=0 swap2 *t=200 *y=200 *x=240 swap2 *t=200 *y=200 *x=240 200 240 500 560 600 A: 0,1 main b=1 a=0 main b=1 a=0 swap2 *t *y=240 *x=200 swap2 *t *y=240 *x=200
9
CPSC 221 Call by value, pointer and reference in C++ Page 9 x=240 t t Attempting to Use Pointers 2 a=0 b=1 main() swap2(int* x, int* y) y=200 600 560 540 200 240 a = 0, b = 1 int* t = x y = t x = y swap2(&a, &b) t=240 x=200 y=240
10
CPSC 221 Call by value, pointer and reference in C++ Page 10 Clicker Question What would be printed to the screen after running the following code? void swap3(int* x, int* y) { int t = *x; *x = *y; *y = t; } int main(){ int a=0; int b=1; swap3(&a,&b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
11
CPSC 221 Call by value, pointer and reference in C++ Page 11 Using Pointers What would be printed to the screen after running the following code? void swap3(int* x, int* y) { int t = *x; *x = *y; *y = t; } int main(){ int a=0; int b=1; swap3(&a,&b); std::cout << a << "," << b; } main b=1 a=0 main b=1 a=0 swap3 t *y=240 *x=200 swap3 t *y=240 *x=200 200 240 500 560 600 B: 1,0 main b=0 a=1 main b=0 a=1 swap3 t=0 *y=240 *x=200 swap3 t=0 *y=240 *x=200
12
CPSC 221 Call by value, pointer and reference in C++ Page 12 x=240 t t Using Pointers a=0 b=1 main() swap3(int* x, int* y) y=200 600 560 540 200 240 a =1, b = 0 int t = *x *y = t *x = *y swap3(&a, &b) t=0 a=1 b=0
13
CPSC 221 Call by value, pointer and reference in C++ Page 13 Clicker Question What would be printed to the screen after running the following code? void swap4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap4(&a,&b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
14
CPSC 221 Call by value, pointer and reference in C++ Page 14 Attempting to Use References What would be printed to the screen after running the following code? void swap4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap4(&a,&b); std::cout << a << "," << b; } C: Does not compile
15
CPSC 221 Call by value, pointer and reference in C++ Page 15 Clicker Question What would be printed to the screen after running the following code? void swap4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap4(a,b); std::cout << a << "," << b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
16
CPSC 221 Call by value, pointer and reference in C++ Page 16 Using References What would be printed to the screen after running the following code? void swap4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap4(a,b); std::cout << a << "," << b; } main b=1 a=0 main b=1 a=0 swap4 t &y=1 &x=0 swap4 t &y=1 &x=0 200 240 500 560 600 B: 1,0 main b=0 a=1 main b=0 a=1 swap4 t=0 &y=0 &x=1 swap4 t=0 &y=0 &x=1 & in a formal parameter makes the parameter another name for the argument that was passed in!
17
CPSC 221 Call by value, pointer and reference in C++ Page 17 x=0 t t Using References a=0 b=1 main() swap4(int &x, int &y) y=1 600 560 540 200 240 a =1, b = 0 int t = x y = t x = y swap4(a, b) t=0 a=1 b=0 x=1 y=0
18
CPSC 221 Call by value, pointer and reference in C++ Page 18 Clicker Question What would be printed to the screen after running the following code? void swap5(int* x, int* y){ int* t = x; x=y; y=t; } int main(){ int* a=new int(0); int* b= new int(1); swap5(a,b); std::cout << *a << "," << *b; } A: 0,1 B: 1,0 C: Does not compile D: I don’t know E: None of the above
19
CPSC 221 Call by value, pointer and reference in C++ Page 19 Attempting to use pointers What would be printed to the screen after running the following code? void swap5(int *x, int* y){ int* t = x; x=y; y=t; } int main(){ int* a=new int(0); int* b= new int(1); swap5(a,b); std::cout << *a << "," << *b; } main *b=950 *a=900 main *b=950 *a=900 swap5 t *y=950 *x=900 swap5 t *y=950 *x=900 200 240 500 560 600 A: 0,1 1 0 900 950 main *b=950 *a=900 main *b=950 *a=900 swap5 t=900 *y=900 *x=950 swap5 t=900 *y=900 *x=950 200 240 500 560 600 1 0 900 950
20
CPSC 221 Call by value, pointer and reference in C++ Page 20 x=900 t t Attempting to Use Pointers a=900 b=950 main() swap5(int* x, int* y) y=950 600 560 540 200 240 a = 0, b = 1 int* t = x y = t x = y swap5(a, b) t=900 x=950 y=900 0 0 1 1 900 950
21
CPSC 221 Call by value, pointer and reference in C++ Page 21 Why use References? Can we do everything without the use of references with just using pointers? –Yes Then why use references? –Because using pointers might sometimes look more complicated. Although you wouldn’t directly be tested on parameter passing, you might get questions on midterm and final that use either pointers or references so you should understand both.
22
CPSC 221 Call by value, pointer and reference in C++ Page 22 Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert1( Node* head, int key) { Node * curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert1( list1, 1); std::cout key; return 0; } A: Yes B: No C: I don’t know
23
CPSC 221 Call by value, pointer and reference in C++ Page 23 Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert1( Node* head, int key) { Node * curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert1( list1, 1); std::cout key; return 0; } B: No main list1=Null main list1=Null insert1 head=Null key=1 insert1 head=Null key=1 200 500 560 key =1 next = Null 950 main list1=Null main list1=Null insert1 curr=950 head=950 key=1 insert1 curr=950 head=950 key=1 200 500 560
24
CPSC 221 Call by value, pointer and reference in C++ Page 24 Attempting to Use Pointers main() insert1(Node* head, int key) 600 200 curr = new Node curr->next = head curr->key = key insert1(list1, 1) curr = 950 list1 = NULL key next key next head = curr 950 head = NULL head = 950 560 key = 1 next key = 1 next key = 1 next = NULL key = 1 next = NULL
25
CPSC 221 Call by value, pointer and reference in C++ Page 25 Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert2( Node** head, int key) { Node * curr = new Node; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list1 = NULL; insert2( &list1, 1); std::cout key; return 0; } A: Yes B: No C: I don’t know
26
CPSC 221 Call by value, pointer and reference in C++ Page 26 Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert2( Node** head, int key) { Node * curr = new Node; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list1 = NULL; insert2( &list1, 1); std::cout key; return 0; } main list1=Null main list1=Null insert2 head=200 key=1 insert2 head=200 key=1 200 500 560 key =1 next = Null 950 main list1=950 main list1=950 insert2 curr=950 head=200 key=1 insert2 curr=950 head=200 key=1 200 500 560 A: Yes
27
CPSC 221 Call by value, pointer and reference in C++ Page 27 Using Pointers main() insert1(Node** head, int key) 600 200 *curr = new Node curr->next = *head curr->key = key insert1(&list1, 1) curr = 950 list1 = NULL key next key next *head = curr 950 head = 200 560 key = 1 next key = 1 next key = 1 next = NULL key = 1 next = NULL list1 = 950
28
CPSC 221 Call by value, pointer and reference in C++ Page 28 Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert3( Node*& head, int key) { Node* curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert3( list1, 1); std::cout key; return 0; } A: Yes B: No C: I don’t know
29
CPSC 221 Call by value, pointer and reference in C++ Page 29 Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert3( Node*& head, int key) { Node* curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert3( list1, 1); std::cout key; return 0; } main list1=Null main list1=Null insert3 &head=Null key=1 insert3 &head=Null key=1 200 500 560 key =1 next = Null 950 main list1=950 main list1=950 insert3 curr=950 &head=950 key=1 insert3 curr=950 &head=950 key=1 200 500 560 A: Yes
30
CPSC 221 Call by value, pointer and reference in C++ Page 30 Using References main() insert3(Node*& head, int key) 600 200 *curr = new Node curr->next = head curr->key = key insert3(list1, 1) curr = 950 list1 = NULL key next key next *head = curr 950 head = NULL 560 key = 1 next key = 1 next key = 1 next = NULL key = 1 next = NULL list1 = 950 head = 950
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.