【发布时间】:2015-05-10 21:49:27
【问题描述】:
我正在做这个练习,我必须编写一个程序来接收一个数字列表并交换成对的数字,以便它们按顺序排列:
void swapPairs(int* a[], int length)
{
int i=0;
int temp;
while(i<(length-1))
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
i++;
}
}
int main()
{
int array[]={2,1,3,1};
swapPairs(array, 4);
return 0;
}
我不断收到这些错误:
In function ‘swapPairs’:
warning: assignment makes integer from pointer without a cast
temp=a[i];
^
warning: assignment makes pointer from integer without a cast
a[i+1]=temp;
In function ‘main’: warning: passing argument 1 of ‘swapPairs’ from incompatible pointer type
swapPairs(array, 4);
^
note: expected ‘int **’ but argument is of type ‘int *’
void swapPairs(int* a[], int length)
^
当我只用一个数组而不是一个指针来尝试它时,它工作得非常好。有人可以解释一下这有什么问题以及如何解决吗?
提前致谢。
【问题讨论】:
-
int* a[]将其更改为int a[]