Suppose we have in the main function of a program two double pointers with valid
ID: 3664613 • Letter: S
Question
Suppose we have in the main function of a program two double pointers with valid addresses stored in them. The purpose of the program is to swap the contents of these pointers using a function call swapPointers. Write the swapPointers function so the program below works properly. It is your choice whether the arguments of swapPointers are passed by value or by reference.
int main()
{
double a = 77.7;
double b = 25.3;
double* p = &a;
double* q = &b;
cout << "content of p: " << p << endl;
cout << "content of q: " << q << endl;
swapPointers( , );
cout << "content of p: " << p << endl;
cout << "content of q: " << q << endl;
return 0;
}
Output:
content of p: 0xff969b08
content of q: 0xff969b00
content of p: 0xff969b00
content of q: 0xff969b08
in c++
Explanation / Answer
Answer:
int main()
{
double a = 77.7;
double b = 25.3;
double* p = &a;
double* q = &b;
cout << "content of p: " << p << endl;
cout << "content of q: " << q << endl;
swapPointers(p , q);
cout << "content of p: " << p << endl;
cout << "content of q: " << q << endl;
return 0;
}
void swapPointers(double *x, double *y)
{
int temp;
temp = *x;
* x = *y;
*y = temp;
return;
}