#include <stdio.h> void swap(int *p1,int *p2); int main() { int a,b,c; int *pointer_1,*pointer_2,*pointer_3; printf("please enter a,b and c"); scanf("%d,%d,%d",&a,&b,&c); pointer_1=&a; pointer_2=&b; pointer_3=&c; if(*pointer_1<*pointer_2){swap(pointer_1,pointer_2); } if(*pointer_2<*pointer_3){swap(pointer_2,pointer_3); } if(*pointer_1<*pointer_2){swap(pointer_1,pointer_2); } printf("这三个数的从大到小的排序为:%d>%d>%d",pointer_1,pointer_2,pointer_3); return 0; } void swap(int *p1,int *p2) { int temp=*p1; *p1=*p2; *p2=temp; }请指出这个代码运行结果为什么错误
时间: 2024-01-23 08:03:21 浏览: 31
这个代码运行结果错误的原因是在输出排序结果时,应该输出指针所指向的值,而不是指针本身。所以应该修改为:
```
printf("这三个数的从大到小的排序为:%d>%d>%d",*pointer_1,*pointer_2,*pointer_3);
```
相关问题
#include <stdio.h> void swap( int *pl,int *p2) { int p; p=*pl; *p1=*p2; *p2=p; } void main() { int a,b; scanf(" %d%d ",&a, &b);
这段代码存在几个问题:
1. 函数 swap 的参数名应该是 p1 和 p2,而非 pl 和 p2。
2. 函数 swap 中的变量名 p 和参数名 p2 重复。
3. 函数 swap 中交换两个变量的语句有误,应该是 *p1 = *p2; *p2 = p;。
4. 函数 main 中没有调用 swap 函数。
5. scanf 函数中的格式化字符串多了一个空格,应该改为 scanf("%d%d", &a, &b);。
修正后的代码如下:
```c
#include <stdio.h>
void swap(int *p1, int *p2) {
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
int main() {
int a, b;
scanf("%d%d", &a, &b);
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
```
输入例如:
```
1 2
```
输出结果为:
```
a = 2, b = 1
```
#include<stdio.h> void swap(int *p1.int *p2) { int *p; p=p1; p1=p2; p2=p; } int main() { int a,b,*pt1,*pt2; scanf("%d,%d",&a,&b); pt1=&a; pt2=&b; if(a<b) swap(pt1;pt2); printf("%d,%d\n",*pt1,*pt2); return 0; }找出错误并修改
有两处错误:
1. 函数 `swap` 的参数类型应该是 `int *p1, int *p2`,而不是 `int *p1.int *p2`。
2. 函数 `swap` 中交换指针指向的值的语句应该是 `int temp = *p1; *p1 = *p2; *p2 = temp;`,而不是 `p=p1; p1=p2; p2=p;`。
修改后的代码如下:
```c
#include <stdio.h>
void swap(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a, b, *pt1, *pt2;
scanf("%d,%d", &a, &b);
pt1 = &a;
pt2 = &b;
if (a < b) {
swap(pt1, pt2);
}
printf("%d,%d\n", *pt1, *pt2);
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)