CPSC 221 Basic Algorithms and Data Structures Call
CPSC 221 Basic Algorithms and Data Structures Call by value, pointer, and reference in C++ Stealing some of the examples from Alan Hassan Khosravi January – April 2015 CPSC 221 Call by value, pointer and reference in C++ Page 1
Clicker Question • What would be printed to the screen after running the following code? void swap 1(int x, int y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap 1(a, b); std: : cout << a << ", " << b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 2
Call By Value • What would be printed to the screen after running the following code? swap 1 void swap 1(int x, int y){ int t = x; x=y; y=t; } 600 t t=0 560 y=1 y=0 500 x=1 int main(){ int a=0; int b=1; swap 1(a, b); std: : cout << a << ", " << b; } main 240 b=1 main b=1 a=0 200 A: 0, 1 CPSC 221 Call by value, pointer and reference in C++ Page 3
Call By Value swap 1(int x, int y) main() 240 200 a=0 600 t t=0 b=1 560 x=1 x=y 540 y=1 y=0 y=t int t = x a = 0, b = 1 CPSC 221 Call by value, pointer and reference in C++ Page 4
Clicker Question • What would be printed to the screen after running the following code? void swap 2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap 2(a, b); std: : cout << a << ", " << b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 5
Attempting to Use Pointers • What would be printed to the screen after running the following code? void swap 2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap 2(a, b); std: : cout << a << ", " << b; } CPSC 221 C: Does not compile Call by value, pointer and reference in C++ Page 6
Clicker Question • What would be printed to the screen after running the following code? void swap 2(int* x, int* y) { int* t = x; x = y; y = t; } int main(){ int a=0; int b=1; swap 2(&a, &b); std: : cout << a << ", " << b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 7
Attempting to Use Pointers 2 • What would be printed to the screen after running the following code? swap 2 void swap 2(int* x, int* y) { int* t = x; x = y; y = t; } 600 int main(){ int a=0; int b=1; swap 2(&a, &b); std: : cout << a << ", " << b; } *t *t=200 560 *y=240 *y=200 500 *x=240 main 240 b=1 main b=1 a=0 200 A: 0, 1 CPSC 221 Call by value, pointer and reference in C++ Page 8
Attempting to Use Pointers 2 main() 240 200 swap 2(int* x, int* y) a=0 t 600 t=240 b=1 x=200 560 x=240 x=y y=200 540 y=240 y=t swap 2(&a, &b) int* t = x a = 0, b = 1 CPSC 221 Call by value, pointer and reference in C++ Page 9
Clicker Question • What would be printed to the screen after running the following code? void swap 3(int* x, int* y) { int t = *x; *x = *y; *y = t; } int main(){ int a=0; int b=1; swap 3(&a, &b); std: : cout << a << ", " << b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 10
Using Pointers • What would be printed to the screen after running the following code? swap 3 void swap 3(int* x, int* y) { int t = *x; *x = *y; *y = t; } 600 int main(){ int a=0; int b=1; swap 3(&a, &b); std: : cout << a << ", " << b; } CPSC 221 t t=0 560 *y=240 500 *x=200 main 240 b=1 main b=0 a=1 200 Call by value, pointer and reference in C++ B: 1, 0 Page 11
Using Pointers swap 3(int* x, int* y) main() 240 200 a=1 600 b=1 560 x=240 *x = *y 540 y=200 *y = t swap 3(&a, &b) t t=0 int t = *x a =1, b = 0 CPSC 221 Call by value, pointer and reference in C++ Page 12
Clicker Question • What would be printed to the screen after running the following code? void swap 4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap 4(&a, &b); std: : cout << a << ", " << b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 13
Attempting to Use References • What would be printed to the screen after running the following code? void swap 4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap 4(&a, &b); std: : cout << a << ", " << b; } CPSC 221 C: Does not compile Call by value, pointer and reference in C++ Page 14
Clicker Question • What would be printed to the screen after running the following code? void swap 4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap 4(a, b); std: : cout << a << ", " << b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 15
Using References • What would be printed to the screen after running the following code? swap 4 void swap 4(int &x, int &y){ int t = x; x=y; y=t; } int main(){ int a=0; int b=1; swap 4(a, b); std: : cout << a << ", " << b; } 600 t 560 &y=1 &y=0 500 &x=1 240 main b=1 main b=0 200 a=1 & in a formal parameter makes the parameter another name for the argument that was passed in! CPSC 221 t=0 Call by value, pointer and reference in C++ B: 1, 0 Page 16
Using References main() 240 200 swap 4(int &x, int &y) a=0 a=1 600 t t=0 b=1 560 x=1 x=0 x=y 540 y=1 y=t swap 4(a, b) int t = x a =1, b = 0 CPSC 221 Call by value, pointer and reference in C++ Page 17
Clicker Question • What would be printed to the screen after running the following code? void swap 5(int* x, int* y){ int* t = x; x=y; y=t; } int main(){ int* a=new int(0); int* b= new int(1); swap 5(a, b); std: : cout << *a << ", " << *b; } CPSC 221 A: 0, 1 B: 1, 0 C: Does not compile D: I don’t know E: None of the above Call by value, pointer and reference in C++ Page 18
Attempting to use pointers • What would be printed to the screen after running the following code? 950 1 void swap 5(int *x, int* y){ int* t = x; x=y; y=t; } 900 int main(){ int* a=new int(0); int* b= new int(1); swap 5(a, b); std: : cout << *a << ", " << *b; } 0 1 900 0 swap 5 600 t 560 *y=950 500 *x=900 swap 5 600 t=900 560 *y=900 500 *x=950 main 240 *b=950 200 *a=900 A: 0, 1 CPSC 221 Call by value, pointer and reference in C++ Page 19
Attempting to Use Pointers swap 5(int* x, int* y) main() a=900 t 600 t=900 200 b=950 x=950 560 x=900 x=y y=950 540 y=900 y=t 240 swap 5(a, b) 900 0 950 1 a = 0, b = 1 CPSC 221 Call by value, pointer and reference in C++ int* t = x Page 20
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. CPSC 221 Call by value, pointer and reference in C++ Page 21
• Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert 1( Node* head, int key) { Node * curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list 1 = NULL; insert 1( list 1, 1); std: : cout << list 1 ->key; return 0; } CPSC 221 Call by value, pointer and reference in C++ A: Yes B: No C: I don’t know Page 22
• Would the following code successfully add a new insert 1 node to the linked list? B: No struct Node { int key; Node* next; }; void insert 1( Node* head, int key) { Node * curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list 1 = NULL; insert 1( list 1, 1); std: : cout << list 1 ->key; return 0; } CPSC 221 Call by value, pointer and reference in C++ 560 500 head=Null key=1 200 main list 1=Null 950 key =1 next = Null 560 500 200 insert 1 curr=950 head=950 key=1 main list 1=Null Page 23
Attempting to Use Pointers insert 1(Node* head, int key) main() 200 list 1 = NULL insert 1(list 1, 1) head==NULL 950 560 head 600 curr = 950 curr = new Node curr->key = key curr->next = head 950 CPSC 221 keykey =1 next = NULL Call by value, pointer and reference in C++ head = curr Page 24
• Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert 2( Node** head, int key) { Node * curr = new Node; curr->key = key; curr->next = *head; *head = curr; } A: Yes B: No C: I don’t know int main(){ Node* list 1 = NULL; insert 2( &list 1, 1); std: : cout << list 1 ->key; return 0; } CPSC 221 Call by value, pointer and reference in C++ Page 25
• Would the following code successfully add a new insert 2 node to the linked list? A: Yes struct Node { int key; Node* next; }; void insert 2( Node** head, int key) { Node * curr = new Node; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list 1 = NULL; insert 2( &list 1, 1); std: : cout << list 1 ->key; return 0; } CPSC 221 560 500 head=200 key=1 200 main list 1=Null 950 key =1 next = Null 560 500 200 Call by value, pointer and reference in C++ insert 2 curr=950 head=200 key=1 main list 1=950 Page 26
Using Pointers insert 1(Node** head, int key) main() 560 list 1==NULL 950 200 list 1 insert 1(&list 1, 1) 600 head = 200 curr = 950 *curr = new Node curr->key = key 950 keykey =1 next = NULL curr->next = *head = curr CPSC 221 Call by value, pointer and reference in C++ Page 27
• Would the following code successfully add a new node to the linked list? struct Node { int key; Node* next; }; void insert 3( Node*& head, int key) { Node* curr = new Node; curr->key = key; curr->next = head; head = curr; } A: Yes B: No C: I don’t know int main(){ Node* list 1 = NULL; insert 3( list 1, 1); std: : cout << list 1 ->key; return 0; } CPSC 221 Call by value, pointer and reference in C++ Page 28
• Would the following code successfully add a new insert 3 node to the linked list? A: Yes struct Node { int key; Node* next; }; void insert 3( Node*& head, int key) { Node* curr = new Node; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list 1 = NULL; insert 3( list 1, 1); std: : cout << list 1 ->key; return 0; } CPSC 221 560 500 &head=Null key=1 200 main list 1=Null 950 key =1 next = Null insert 3 curr=950 560 &head=950 500 key=1 200 Call by value, pointer and reference in C++ main list 1=950 Page 29
Using References insert 3(Node*& head, int key) main() list 1==NULL 950 200 list 1 insert 3(list 1, 1) head==NULL 950 560 head 600 curr = 950 *curr = new Node curr->key = key 950 keykey =1 next = NULL curr->next = head *head = curr CPSC 221 Call by value, pointer and reference in C++ Page 30
- Slides: 30