#include<stdio.h> void swap(int x,int y) { int t; t=x; x=y; y=t; } int main() { int a=3,b=7; swap(3,7); printf("a=%d\n",a); printf("b=%d\n",b); }想要交换ab的结果怎修改
时间: 2023-12-02 22:05:43 浏览: 115
要修改 swap 函数的参数为指针或引用,这样才能实现对变量的交换。改动后的代码如下:
```c
#include<stdio.h>
void swap(int *x, int *y) {
int t = *x;
*x = *y;
*y = t;
}
int main() {
int a = 3, b = 7;
swap(&a, &b);
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}
```
在 main 函数中调用 swap 函数时,传入的是变量 a 和 b 的地址,swap 函数中的参数也改为指针类型,通过指针操作实现对变量的交换。输出结果为:
```
a=7
b=3
```
可以看到,a 和 b 的值已经成功交换了。
相关问题
修正#include<stdio.h> void swap(int a,int b) { int temp =a; a=b; b=temp; } int main() { int x=10,y=20 printf("before swap;x=%d,y=%d",x,y); swap (&x,&y); printf("after swap;x=%d,y=%d",x,y); return 0 }
修正后的代码如下,主要是将 swap 函数的参数改为指针类型,并在调用 swap 函数时传入 x 和 y 的地址:
```c
#include<stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("before swap; x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("after swap; x=%d, y=%d\n", x, y);
return 0;
}
```
输出结果为:
```
before swap; x=10, y=20
after swap; x=20, y=10
```
可以看到,经过 swap 函数交换后,x 和 y 的值已经被成功交换了。
#include <stdio.h> #include <stdlib.h> void Swap(int x, int y){ int tmp; tmp = x; x = y; y = tmp; } int main(){ int num1 = 10; int num2 = 20; Swap(num1,num2); printf("num1 = %d,num2 = %d\n",num1,num2); } 为什么这个程序输出结果不交换,原因及多种正确代码表示
这个程序输出结果不交换的原因是因为在 Swap 函数中,传入的参数是按值传递(pass by value),也就是说,在函数内部修改的是传入参数的副本,而不是传入参数本身。因此,函数内部的交换操作并不会影响到调用函数时传入的参数。要解决这个问题,可以使用指针或者引用作为函数参数,这样就可以修改传入参数的值。以下是两种正确的代码表示:
使用指针:
```c
#include <stdio.h>
#include <stdlib.h>
void Swap(int *x, int *y){
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int main(){
int num1 = 10;
int num2 = 20;
Swap(&num1,&num2);
printf("num1 = %d,num2 = %d\n",num1,num2);
}
```
使用引用:
```c
#include <stdio.h>
#include <stdlib.h>
void Swap(int &x, int &y){
int tmp;
tmp = x;
x = y;
y = tmp;
}
int main(){
int num1 = 10;
int num2 = 20;
Swap(num1,num2);
printf("num1 = %d,num2 = %d\n",num1,num2);
}
```
在以上两种情况下,传入的参数都可以被正确的交换。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)