100 likes | 205 Views
Tracing through E01, question 9 – step 1. // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; }
E N D
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main
Tracing through E01, question 9 – step 2 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } 7 k main
Tracing through E01, question 9 – step 3 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } mysteryFunction() 7 k main()
Tracing through E01, question 9 – step 4 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } The address of k is copied into xSince an address is a pointer,and a pointer is an address,x now points to k. x mysteryFunction() 7 k main()
Tracing through E01, question 9 – step 5 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } result x mysteryFunction() 7 k main()
Tracing through E01, question 9 – step 6 (*x) means dereference the pointer xto go to what it points to. That is, k. Since (*x) is really k, the value of k, which is 7, gets copied into result. // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } result 7 x mysteryFunction() 7 k main()
Tracing through E01, question 9 – step 7 result gets multiplied by 3,and 7 changes to 21 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } result 7 21 x mysteryFunction() 7 k main()
Tracing through E01, question 9 – step 8 Now, (*x) is on the left hand side. (*x) dereferences the pointer x, follows the arrow to what is pointed to, which is k. So (*x) on the left hand side, really means that we are assigning a new value to k. // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } result 21 x mysteryFunction() 7 21 k main()
Tracing through E01, question 9 – step 9 When we reach the end of the function, the call frame, and all of its local variables are popped off of the stack, and they go away. // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } result 21 x mysteryFunction() 21 k main()
Tracing through E01, question 9 – step 10 We have returned from the function, and we print the answer, 21. // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include <iostream> using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } 21 k main()