c语言用指针和自定义函数实现数组元素的调换 20
1个回答
展开全部
#include <stdio.h>
#define N 10
void Swap ( int *a, int *b, int n);
int main(void)
{
int *a, *b;
int num1[N], num2[N];
for (a = num1; a < num1 + N; a++)
scanf ("%d", a);
for (b = num2; b < num2 + N; b++)
scanf ("%d", b);
a = num1;//重新指向首地址
b = num2;//重新指向首地址
Swap (a, b, N);
printf ("*********交换后**********\n");
for (a = num1; a < num1 + N; a++)
printf ("%3d", *a);
printf ("\n");
for (b = num2; b < num2 + N; b++)
printf ("%3d", *b);
return 0;
}
void Swap (int *a, int *b, int n)
{
int temp;
int i;
for (i = 0; i < n; i++)
{
temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}
#define N 10
void Swap ( int *a, int *b, int n);
int main(void)
{
int *a, *b;
int num1[N], num2[N];
for (a = num1; a < num1 + N; a++)
scanf ("%d", a);
for (b = num2; b < num2 + N; b++)
scanf ("%d", b);
a = num1;//重新指向首地址
b = num2;//重新指向首地址
Swap (a, b, N);
printf ("*********交换后**********\n");
for (a = num1; a < num1 + N; a++)
printf ("%3d", *a);
printf ("\n");
for (b = num2; b < num2 + N; b++)
printf ("%3d", *b);
return 0;
}
void Swap (int *a, int *b, int n)
{
int temp;
int i;
for (i = 0; i < n; i++)
{
temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询