300 likes | 317 Views
CPSC 221 Basic Algorithms and Data Structures. Call by value, pointer, and reference in C++ Stealing some of the examples from Alan. 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; }
E N D
CPSC 221Basic Algorithms and Data Structures Call by value, pointer, and reference in C++Stealing some of the examples from Alan
Clicker Question • What would be printed to the screen after running the following code? void swap1(int x, int y){ intt = x; x=y; y=t; } intmain(){ 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
Call By Value • What would be printed to the screen after running the following code? swap1 t y=1 x=0 swap1 t=0 y=0 x=1 void swap1(int x, int y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap1(a,b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=1 a=0 240 200 A: 0,1
Call By Value swap1(int x, int y) main() a=0 t t=0 int t = x 600 240 b=1 x=0 x=1 200 560 x = y y=1 y=0 y = t 540 a = 0, b = 1
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; } intmain(){ 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
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; } intmain(){ int a=0; int b=1; swap2(a,b); std::cout << a << "," << b; } C: Does not compile
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; } intmain(){ 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
Attempting to Use Pointers 2 • What would be printed to the screen after running the following code? swap2 *t *y=240 *x=200 swap2 *t=200 *y=200 *x=240 void swap2(int* x, int* y) { int* t = x; x = y; y = t; } intmain(){ int a=0; int b=1; swap2(&a,&b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=1 a=0 240 200 A: 0,1
Attempting to Use Pointers 2 swap2(int* x, int* y) main() a=0 t t=240 int* t = x 600 240 b=1 x=240 x=200 200 560 x = y swap2(&a, &b) y=240 y=200 y = t 540 a = 0, b = 1
Clicker Question • What would be printed to the screen after running the following code? void swap3(int* x, int* y) { intt = *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
Using Pointers • What would be printed to the screen after running the following code? swap3 t *y=240 *x=200 swap3 t=0 *y=240 *x=200 void swap3(int* x, int* y) { intt = *x; *x = *y; *y = t; } int main(){ int a=0; int b=1; swap3(&a,&b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=0 a=1 240 200 B: 1,0
Using Pointers swap3(int* x, int* y) main() a=0 a=1 t t=0 int t = *x 600 240 b=0 b=1 x=240 200 560 *x = *y swap3(&a, &b) y=200 *y = t 540 a =1, b = 0
Clicker Question • What would be printed to the screen after running the following code? void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ 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
Attempting to Use References • What would be printed to the screen after running the following code? void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap4(&a,&b); std::cout << a << "," << b; } C: Does not compile
Clicker Question • What would be printed to the screen after running the following code? void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ 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
Using References • What would be printed to the screen after running the following code? swap4 t &y=1 &x=0 swap4 t=0 &y=0 &x=1 void swap4(int &x, int &y){ intt = x; x=y; y=t; } intmain(){ int a=0; int b=1; swap4(a,b); std::cout << a << "," << b; } 600 560 500 main b=1 a=0 main b=0 a=1 240 200 & in a formal parameter makes the parameter another name for the argument that was passed in! B: 1,0
Using References swap4(int &x, int &y) main() a=0 a=1 t t=0 int t = x 600 240 b=0 b=1 x=1 x=0 200 560 x = y swap4(a, b) y=0 y=1 y = t 540 a =1, b = 0
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; } intmain(){ int* a=newint(0); int* b= newint(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
Attempting to use pointers • What would be printed to the screen after running the following code? 950 950 1 1 void swap5(int *x, int* y){ int* t = x; x=y; y=t; } intmain(){ int* a=newint(0); int* b= newint(1); swap5(a,b); std::cout << *a << "," << *b; } 900 900 0 0 swap5 t *y=950 *x=900 swap5 t=900 *y=900 *x=950 600 600 560 560 500 500 main *b=950 *a=900 main *b=950 *a=900 240 240 200 200 A: 0,1
Attempting to Use Pointers swap5(int* x, int* y) main() a=900 t t=900 int* t = x 600 240 b=950 x=900 x=950 200 560 x = y swap5(a, b) y=900 y=950 y = t 540 0 900 a = 0, b = 1 1 950
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.
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 = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert1( list1, 1); std::cout << list1->key; return 0; } A: Yes B: No C: I don’t know
B: No insert1 head=Null key=1 560 • 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 = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert1( list1, 1); std::cout << list1->key; return 0; } 500 main list1=Null key =1 next = Null 200 950 insert1 curr=950 head=950 key=1 560 500 main list1=Null 200
Attempting to Use Pointers insert1(Node* head, int key) main() head = 950 head = NULL 560 list1 = NULL 200 curr = new Node insert1(list1, 1) curr = 950 600 curr->key = key curr->next = head key next head = curr key = 1 next key = 1 next = NULL 950
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 = newNode; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list1 = NULL; insert2( &list1, 1); std::cout << list1->key; return 0; } A: Yes B: No C: I don’t know
A: Yes insert2 head=200 key=1 560 • 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 = newNode; curr->key = key; curr->next = *head; *head = curr; } int main(){ Node* list1 = NULL; insert2( &list1, 1); std::cout << list1->key; return 0; } 500 main list1=Null key =1 next = Null 200 950 insert2 curr=950 head=200 key=1 560 500 main list1=950 200
Using Pointers insert1(Node** head, int key) main() head = 200 560 list1 = NULL list1 = 950 200 insert1(&list1, 1) curr = 950 600 *curr = new Node curr->key = key key next key = 1 next key = 1 next = NULL curr->next = *head 950 *head = curr
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 = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert3( list1, 1); std::cout << list1->key; return 0; } A: Yes B: No C: I don’t know
A: Yes insert3 &head=Null key=1 560 • 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 = newNode; curr->key = key; curr->next = head; head = curr; } int main(){ Node* list1 = NULL; insert3( list1, 1); std::cout << list1->key; return 0; } 500 main list1=Null key =1 next = Null 200 950 insert3 curr=950 &head=950 key=1 560 500 main list1=950 200
Using References insert3(Node*& head, int key) main() head = NULL head = 950 560 list1 = NULL list1 = 950 200 insert3(list1, 1) curr = 950 600 *curr = new Node curr->key = key key next key = 1 next key = 1 next = NULL curr->next = head 950 *head = curr