为什么会出现细分11错误?

时间:2020-02-28 22:53:58

标签: c sorting pointers memory segmentation-fault

#include <stdio.h>

int* selectsort(int* ptr, int length);

int main()
{
    int array[] = {3, 4, 0, 2, 9, 6};
    int* ptr = array;
    int length = sizeof(array);
        // printf("%i\n", sizeof(*ptr));
    int* h = selectsort(ptr, length);
    for (int a = 0; a < length; a++)
    {
        printf("%i\n", *(h+a));
    }
    printf("hello\n");

}


int* selectsort(int* ptr, int length)
{
    int sortedarray[length];
    int* sorted = sortedarray;
    int smallest = 0;
    for (int i = 0; i < length; i++) {
            for (int z = 0; i < length; z++) {
                    if (*(ptr+i) > *(ptr+z)) {
                            smallest = *(ptr+i);
                    } else
                    {
                            smallest = *(ptr+z);
                    }
            } sortedarray[i] = smallest;
    }
    return sorted;
}

嘿,在某种程度上我遇到了分段错误,但是我找不到错误。可能必须对内存做些事... 如您所知,我刚接触c,所以如果有人可以帮助我,那将非常好:D 感谢您的帮助!

0 个答案:

没有答案