单选题:下列哪个代码能够实现x和y值的交换( )
下列哪个代码能够实现x和y值的交换( )
A.c++
void fun(int a,int b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(&x, &y);
cout << x << ","<<y << endl;
return 0;
}
B.c++
void fun(int* a,int* b) {
int x = *a;
*a = *b;
*b = x;
}
int main() {
int x = 1, y = 2;
fun(&x,&y);
cout << x << ","<<y << endl;
return 0;
}
C.c++
void fun(int& a,int& b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(&x, &y);
cout << x << ","<<y << endl;
return 0;
}
D.c++
void fun(const int&a,const int&b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(x, y);
cout << x << ","<<y << endl;
return 0;
}
answer:B
A.c++
void fun(int a,int b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(&x, &y);
cout << x << ","<<y << endl;
return 0;
}
B.c++
void fun(int* a,int* b) {
int x = *a;
*a = *b;
*b = x;
}
int main() {
int x = 1, y = 2;
fun(&x,&y);
cout << x << ","<<y << endl;
return 0;
}
C.c++
void fun(int& a,int& b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(&x, &y);
cout << x << ","<<y << endl;
return 0;
}
D.c++
void fun(const int&a,const int&b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(x, y);
cout << x << ","<<y << endl;
return 0;
}
answer:B