我要执行的最后一个任务是将数组的第一个元素(数组[0]与数组的最后一个元素(数组[2])的值交换;但是,每当编译时,我收到这三个错误,似乎无法修复它们:
:4 redefinition of parameter 'array'
和
:32 incompatible type for argument 1 of 'swap'
:32 incompatible type for argument 2 of 'swap'
这是我的代码:
#include <stdio.h>
void
swap(double *array[0],double *array[2])
{
int temp = *array[0];
*array[0] = *array[2];
*array[2] = temp;
}
int
main(int argc, char **argv)
{
double array[3] = {0};
double realNumber;
printf("array[0] is %f\n",array[0]);
printf("array[1] is %f\n",array[1]);
printf("array[2] is %f\n",array[2]);
printf("enter the first real number:\n");
scanf("%lf",&realNumber);
array[0] = realNumber;
printf("enter the second real number:\n");
scanf("%lf",&realNumber);
array[1] = realNumber;
printf("enter the third real number:\n");
scanf("%lf",&realNumber);
array[2] = realNumber;
printf("array[0] is %f\n",array[0]);
printf("array[1] is %f\n",array[1]);
printf("array[2] is %f\n",array[2]);
swap(double array[0],double array[2]);
printf("after swapping...\n");
printf("array[0] is %f\n",array[0]);
printf("array[1] is %f\n",array[1]);
printf("array[2] is %f\n",array[2]);
return 0;
}
最佳答案
使用此…更新的代码。。。
#include <stdio.h>
void swap(double *a,double *b)
{
double temp = *a;
*a = *b;
*b = temp;
}
int main(int argc, char **argv)
{
double array[3] = {0};
double realNumber;
printf("array[0] is %lf\n",array[0]);
printf("array[1] is %lf\n",array[1]);
printf("array[2] is %lf\n",array[2]);
printf("enter the first real number:\n");
scanf("%lf",&realNumber);
array[0] = realNumber;
printf("enter the second real number:\n");
scanf("%lf",&realNumber);
array[1] = realNumber;
printf("enter the third real number:\n");
scanf("%lf",&realNumber);
array[2] = realNumber;
printf("array[0] is %lf\n",array[0]);
printf("array[1] is %lf\n",array[1]);
printf("array[2] is %lf\n",array[2]);
swap(array,array+2);
printf("after swapping...\n");
printf("array[0] is %lf\n",array[0]);
printf("array[1] is %lf\n",array[1]);
printf("array[2] is %lf\n",array[2]);
return 0;
}