Ok then theres one more question also concerning pointers, this one I actually compiled and ran. However I get the same 'return' values for both programs. I do know they represent the same thing but the question wants it otherwise.
John is tasked with writing a function, called swap, that will take two pointers to integers as parameters
and swap the values at the memory locations they point to. Somewhat baed by the request, he asks
his friend Joan for help. She gives him one of her old homework assignments, where the task was
simply to swap two integer values. Her answer looked as follows:
1 2 3 4 5
|
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
|
John adjusts Joan's function to match his own specications by changing every instance of int to int *. The resultant function is:
1 2 3 4 5
|
void swap(int *&a, int *&b) {
int *tmp = a;
a = b;
b = tmp;
}
|
Then the questions
(a) What effect will John's code actually have? Contrast the real behaviour with the expected behaviour.
I said It does swap a with b??
(b) Re-write the body of the function so that it will work correctly. Leave the function signature as
(c) After John has correctly implemented the body of the function, does he still need to pass the parameters by reference? In other words, should he leave the formal parameter list as (int *&a, int *&b), or can he remove the ampersands now? Only say whether reference passing is necessary.
(d) Explain your answer to (c). To do this, rst explain why Joan's function required passing by reference. Then explain whether this applies to John's code, and how the purpose of passing by reference is similar or dierent in John's function.
GUYS I really appreciate It... And at the same time learning the correct jargon that goes with it