c:段错误(选择排序)

标签 c arrays sorting

#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/

相关文章:

c++ - 如何使用 C++ 成员函数作为 C 框架的回调函数

c++ - 将具有可变边界的二维数组传递给函数

c++ - 检查两个数组是否相同的函数

java - 一类中有两个比较器

r - 如何在R中对列表进行排序?

c - 使用双指针对字符串中的字母进行排序

c - 如何重复数组中的字符

c - C中5点加权平均的最优算法

arrays - 在范围和计数的散列中对整数数组进行分组

php - 帮助我使用 usort() 对这个 php 数组进行排序