#include <stdio.h> #define N 10 void ReadData(int a[], int n); void PrintData(int a[], int n); void Swap(int *x, int *y); int main() { int a[N], b[N], i, n; printf("Input array size(n<=10):"); scanf("%d", &n); printf("Input array a:"); ReadData(a, n); printf("Input array b:"); ReadData(b, n); int *x=a,*y=b; Swap(*x, *y); printf("Output array a:"); PrintData(a, n); printf("Output array b:"); PrintData(b, n); return 0; } void ReadData(int a[], int n) { int i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } } void PrintData(int a[], int n) { int i; for (i = 0; i < n; i++) { printf("%5d", a[i]); } printf("\n"); } /* 函数功能:两整数值互换 */ void Swap(int *x, int *y) { int temp,n; do { temp=x; x=y; y=x; x++; y++; }while(x<n&&y<n); }
时间: 2024-01-26 10:03:19 浏览: 45
这段代码存在一些错误,具体如下:
1. 在函数`Swap`中,形参`x`和`y`都是指针类型,但是在函数体内部却将它们当成了整型变量使用,这是错误的。应该使用`*x`和`*y`来访问它们所指向的变量。
2. 在函数`Swap`中,循环条件`while(x<n&&y<n)`中的变量`n`未定义,应该使用函数参数`n`。
3. 在函数`main`中,将指向数组`a`和`b`的指针`x`和`y`分别赋值为`a`和`b`,然后再将它们作为参数传递给函数`Swap`,但是函数`Swap`中的参数类型是指针类型,应该直接将`x`和`y`作为参数传递给函数`Swap`。
下面是修改后的代码:
```c
#include <stdio.h>
#define N 10
void ReadData(int a[], int n);
void PrintData(int a[], int n);
void Swap(int *x, int *y);
int main() {
int a[N], b[N], i, n;
printf("Input array size(n<=10):");
scanf("%d", &n);
printf("Input array a:");
ReadData(a, n);
printf("Input array b:");
ReadData(b, n);
int *x = a, *y = b;
Swap(x, y);
printf("Output array a:");
PrintData(a, n);
printf("Output array b:");
PrintData(b, n);
return 0;
}
void ReadData(int a[], int n) {
int i;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
}
void PrintData(int a[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%5d", a[i]);
}
printf("\n");
}
/* 函数功能:两整数值互换 */
void Swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![application/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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)