1 / 10

Tracing through E01, question 9 – step 1

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; }

sissy
Download Presentation

Tracing through E01, question 9 – step 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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()

  4. 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()

  5. 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()

  6. 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()

  7. 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()

  8. 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()

  9. 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()

  10. 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()

More Related