Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.

Similar presentations


Presentation on theme: "Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int."— Presentation transcript:

1 Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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 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 7 k

3 Tracing through E01, question 9 – step 3 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 7 k mysteryFunction()

4 Tracing through E01, question 9 – step 4 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 7 k mysteryFunction() x The address of k is copied into x Since an address is a pointer, and a pointer is an address, x now points to k.

5 Tracing through E01, question 9 – step 5 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 7 k mysteryFunction() x result

6 Tracing through E01, question 9 – step 6 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 7 k mysteryFunction() x 7 result (*x) means dereference the pointer x to 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.

7 Tracing through E01, question 9 – step 7 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 7 k mysteryFunction() x 7 21 result result gets multiplied by 3, and 7 changes to 21

8 Tracing through E01, question 9 – step 8 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 7 21 k mysteryFunction() x 21 result 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.

9 Tracing through E01, question 9 – step 9 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 21 k mysteryFunction() x 21 result 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.

10 Tracing through E01, question 9 – step 10 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include 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() 21 k We have returned from the function, and we print the answer, 21.


Download ppt "Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int."

Similar presentations


Ads by Google