C语言 数组元素的交换函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25925603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swap function of elements in array
提问by libbned
the final task for me to perform is to swap the value of the first element of my array (array[0] with the last element of my array (array[2]); however, whenever i compile i receive these three errors and cannot seem to fix them:
我要执行的最后一项任务是交换数组的第一个元素的值(数组 [0] 与数组的最后一个元素(数组 [2]);但是,每当我编译时,我都会收到这三个错误,并且不能似乎修复它们:
:4 redefinition of parameter 'array'
and
和
:32 incompatible type for argument 1 of 'swap'
:32 incompatible type for argument 2 of 'swap'
here is my code:
这是我的代码:
#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;
}
采纳答案by MD. Khairul Basar
use this...updated code...
使用这个...更新的代码...
#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;
}
回答by Elliott Frisch
I strongly suspect that this,
我强烈怀疑这个,
void
swap(double *array[0],double *array[2])
{
int temp = *array[0];
*array[0] = *array[2];
*array[2] = temp;
}
Should be
应该
void
swap(double *array, int a, int b)
{
double temp = *array[a]; /* <- it's a double */
*array[a] = *array[b];
*array[b] = temp;
}
And to call it, this
并称之为,这
swap(double array[0],double array[2]);
should be
应该
swap(array,0,2);
finally, if you prefer, pass in two pointers with the temp variable and call it with swap(array[0], array[2]),
最后,如果您愿意,可以使用 temp 变量传入两个指针并使用swap(array[0], array[2]),
void swap(double *a, double *b)
{
double temp = *a;
*a = *b;
*b = temp;
}
回答by Abhishek Choubey
redefinition of parameter 'array' :
You are getting this error because of the following function call :
由于以下函数调用,您收到此错误:
Error 1 :
swap(double array[0],double array[2]);
We never pass the data type with the variable while invoking a function, just pass the
parameters.
我们在调用函数时从不通过变量传递数据类型,只传递
参数。
Correction :
swap(array[0],array[2]); // if you want to pass the values as parameters.
//or
swap(array, array); // for passing pointers to the array( pass by reference )
You can read about pass by value and pass by reference. A point to be noted that in C
the arrays are always passed by reference.
您可以阅读有关按值传递和按引用传递的信息。需要注意的一点是,在 C
中数组总是通过引用传递。
Error 2:
incompatible type for argument 1 of 'swap' :32 incompatible type for argument
2 of 'swap' :
This error comes because the prototype of swap function given by you is
这个错误是因为你给的swap函数原型是
void swap(double *array[0],double *array[2]);
That's not a standard way of giving function prototypes.
- But when you are calling the function you are passing only double values, instead you should be passing the address of the double array as per your prototype definition.
Correction :
- prototype declaration :
void swap(double *, double *); // pass by reference
void swap(double, double); // pass by value
这不是给出函数原型的标准方式。
- 但是,当您调用该函数时,您只传递双精度值,而应该根据原型定义传递双精度数组的地址。
更正:
- 原型声明:
void swap(double *, double *); // 通过引用传递
void swap(double, double); // 传值
2.Mapping correct prototypes with their respective function calls:
2.映射正确的原型与其各自的函数调用:
If you want to pass the base address of the array as argument :
prototype : void swap(double *, double *);
call : swap(array, array);
But here I suggest you need not pass two parameters, if they are same.
但在这里我建议你不需要传递两个参数,如果它们是相同的。
If you want to pass a specific value stored in array at some index say 'i':
prototype : void swap(double, double);
call : swap(array[i], array[j]);
Where 'i' and 'j' are indexes to the array.
其中 'i' 和 'j' 是数组的索引。
回答by nem035
Each time you add a type in front of a variable name you are declaring a new variable of that type and with that name.
每次在变量名前添加类型时,都在声明该类型并具有该名称的新变量。
So you declare array at the top of main():
所以你在顶部声明数组main():
double array[3] = {0};
// ... some code in between
swap(double array[0],double array[2]); // here you re-declare it
You don't need the doublehere because the program already knows array contains doubles. That is why you are getting the redefinition of parameter 'array'error.
Remove both doubletypes in the second statement:
你不需要double这里,因为程序已经知道数组包含双打。这就是您收到redefinition of parameter 'array'错误的原因。删除double第二个语句中的两种类型:
double array[3] = {0};
// ... some code in between
swap(array[0], array[2]);
Also, your array contains only one element, namely 0. But you want to swap the first array[0]and the third array[2]element which doesn't exist. Create a bigger array, something like:
此外,您的数组仅包含一个元素,即0. 但是您想交换不存在的第一个array[0]和第三个array[2]元素。创建一个更大的数组,例如:
double array[3] = {0, 1, 2};
// ... some code in between
swap(array[0], array[2]);
Furthermore, your definition of the swapfunction is wrong too.
此外,您对swap函数的定义也是错误的。
void
swap(double *array[0],double *array[2])
{
int temp = *array[0];
*array[0] = *array[2];
*array[2] = temp;
}
You don't need the [position]part as the parameter. This function is generic and shouldn't be allowed to only swap array[0]and array[2]. All you need here is a double *because array[position]is a doubleand that is what you are passing into the function. You are passing this value by reference (that is why you have the *) so that the swap will actually work outside the function and not just swap locally.
您不需要该[position]部分作为参数。此函数是通用的,不应只array[0]array[2]允许交换和。这里你需要的只是一个double *因为array[position]是一个double,这就是你传递给函数的内容。您通过引用传递此值(这就是您拥有 的原因*),以便交换实际上在函数之外工作,而不仅仅是在本地交换。
Read more about pointer/references in C
This is what you should have:
这是你应该拥有的:
void swap(double *number1, double *number2)
{
int *temp = number1;
number1 = number2;
number2 = temp;
}

