#include<stdio.h>
void selsort(int a[],int s)
{
int min,temp,i,j;
for(i = 0; i < s ; i++)
{
min = 0;
for(j = i+1 ; j < s; j++)
{
if(min > a[j])
{
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
for (j=0;j<s;j++)
{
printf("%d",a[i]);
}
printf("\n");
}
}
main()
{
int i,a[5];
int size = 5;
printf("Enter elements of the array");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
selsort(a[5],size);
}
错误如下:
selsort.c:35:2: warning: passing argument 1 of ‘selsort’ makes pointer from integer without a cast [enabled by default]
selsort.c:2:1: note: expected ‘int *’ but argument is of type ‘int’
有关如何避免将来出现此问题的任何提示都会非常有帮助
最佳答案
您应该像这样调用您的函数:
selsort(a, size);
a[5]
表示“索引 5 处的元素”(顺便说一下,超出数组末尾:int a[5]
中最大的合法元素位于索引 4
处)。
您还应该更换
min = 0;
与
min = i;
并像这样使用它:
if(a[min] > a[j]) ...
关于c:段错误(选择排序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888678/