Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用指针对C中的结构进行排序_C_Sorting_Pointers - Fatal编程技术网

用指针对C中的结构进行排序

用指针对C中的结构进行排序,c,sorting,pointers,C,Sorting,Pointers,我刚开始学C,对幕后发生的事情几乎一无所知。我正在为一个数据结构类学习它,这让事情变得更难 更新:我已经把程序精简了,我从内存开始,然后再从上开始。我在其中有分配和取消分配函数,我得到了一个malloc错误:Q1(9882)malloc:*对象0x7fff59daec08的错误:未分配被释放的指针 *在malloc\u error\u break中设置断点以进行调试 更新2这是我修改过的代码,它仍然缺少一些东西,一些printf语句没有出现: #include <stdio.h> #

我刚开始学C,对幕后发生的事情几乎一无所知。我正在为一个数据结构类学习它,这让事情变得更难

更新:我已经把程序精简了,我从内存开始,然后再从上开始。我在其中有分配和取消分配函数,我得到了一个malloc错误:Q1(9882)malloc:*对象0x7fff59daec08的错误:未分配被释放的指针 *在malloc\u error\u break中设置断点以进行调试

更新2这是我修改过的代码,它仍然缺少一些东西,一些printf语句没有出现:

#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<assert.h>

static int size = 10;

struct student{
    int id;
    int score;
};

struct student* allocate(){
     /*Allocate memory for ten students*/
     struct student *s = malloc(size*(sizeof(struct student)));
     assert(s != 0);
     /*return the pointer*/
     return s;
}

void generate(struct student* students){
    /*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0 and 100*/
    srand((unsigned int)time(NULL));
    int id[size];
    int y;

    for (int i = 0; i < size; i++){
        y = rand() % size + 1;
        while(dupe(id, i, y)){
            y = rand() % size + 1;
        }
        id[i] = y;
    }

    for (int j = 0; j < size; j++){
        (students + j)->id = id[j];
        (students + j)->score = rand() % 101;
        printf("ID: %d\tScore: %d\n", (students + j)->id, (students + j)->score);
    }
}

int dupe(int id[], int size1, int i){
    for (int x = 0; x < size1; x++){
        if(id[x] == i)
            return 1;
    }
    return 0;
}

void output(struct student* students){
     /*Output information about the ten students in the format:
              ID1 Score1
              ID2 score2
              ID3 score3
              ...
              ID10 score10*/
    sort(&students);
    for(int x = 0; x < size; x++){
        printf("ID: %d\tScore: %d\n", (students + x)->id, (students + x)->score); //print stmt not showing
    }
}

void sort(struct student* students){
    struct student *sd = allocate();

    struct student *stud;

    for(int i = 0; i < size; i++){
        stud = &students[i];
        sd[stud->id] = *stud;
    }
    for(int x = 0; x < size; x++){
        printf("ID: %d\tScore: %d\n", (sd + x)->id, (sd + x)->score); //print stmt not showing
    }
    students = &sd;
    deallocate(sd);
}

void summary(struct student* students){
     /*Compute and print the minimum, maximum and average scores of the ten students*/

}

void deallocate(struct student* stud){
     /*Deallocate memory from stud*/
    free(stud);
}

int main(){
    struct student* stud = NULL;
    char c[] = "------------------------------\n";
    /*call allocate*/
    stud = allocate();
    /*call generate*/
    generate(&stud);
    /*call output*/
    printf("%s", c);
    output(&stud);
    /*call summary*/

    /*call deallocate*/
    deallocate(stud);

    return 0;
}
#包括
#包括
#包括
#包括
静态整数大小=10;
结构学生{
int-id;
智力得分;
};
结构学生*分配(){
/*给十个学生分配内存*/
struct student*s=malloc(size*(sizeof(struct student));
断言(s!=0);
/*返回指针*/
返回s;
}
无效生成(结构学生*学生){
/*为10名学生生成随机ID和分数,ID介于1和10之间,分数介于0和100之间*/
srand((无符号整数)时间(NULL));
int id[大小];
int-y;
对于(int i=0;iid=id[j];
(学生+j)->分数=兰德()%101;
printf(“ID:%d\t原因:%d\n”,(学生+j)->ID,(学生+j)->分数);
}
}
重复整数(整数id[],整数大小1,整数i){
对于(int x=0;xID,(学生+x)->分数);//打印stmt不显示
}
}
无效排序(结构学生*学生){
struct student*sd=allocate();
结构学生*螺柱;
对于(int i=0;iid]=*螺柱;
}
用于(int x=0;xID,(sd+x)->分数);//打印stmt不显示
}
学生=&sd;
解除分配(sd);
}
无效摘要(结构学生*学生){
/*计算并打印十名学生的最低、最高和平均分数*/
}
无效解除分配(结构学生*student){
/*从螺柱释放内存*/
自由(螺柱);
}
int main(){
struct student*stud=NULL;
字符c[]=“-----------------------------------\n”;
/*呼叫分配*/
螺柱=分配();
/*呼叫生成*/
生成(&stud);
/*呼叫输出*/
printf(“%s”,c);
输出(&螺柱);
/*电话摘要*/
/*呼叫解除分配*/
解除分配(螺柱);
返回0;
}
这句话

students = &students[x];
修改传入参数。您丢失了“学生”所指的内容,即
struct student[]
的开头

请删除此语句,然后再次尝试运行程序

还有其他错误,但这应该会让你摆脱困境

指针是硬的。

这句话

students = &students[x];
students = &students[x];
修改传入参数。您丢失了“学生”所指的内容,即
struct student[]
的开头

请删除此语句,然后再次尝试运行程序

还有其他错误,但这应该会让你摆脱困境

指针是硬的

students = &students[x];
这会改变
学生
点的位置,因此下次通过循环时,您将从那里开始偏移,而不是从一开始。也就是说,您将获得
原始学生[0]
原始学生[1]
原始学生[1+2]
原始学生[1+2+3]
,等等。您在
sd
方面也有同样的问题

相反,您希望使用不同的变量,例如

struct student* st = &students[x];
printf("id = %d\tscore = %d\n", st->id, st->score);
etc
还有,sd的用途是什么?你似乎无缘无故地分配了一些空间并将学生复制到sd。分配的空间未保存或返回。。。这是内存泄漏。哦,等等,我明白了。。你在sd中按照学生id的顺序对学生重新排序。所以你应该在完成后释放内存。但是对于student和sd,您需要一个指向数组的指针,而不是指向数组元素的指针。您可以使用许多不同的命名约定,但最好使用一致的命名约定。例如:

void output(struct Student* students){
    struct Student *idstudents = allocate(); /* sorted by id */
    if (!idstudents)
        /* handle allocation error */;

    for (int x = 0; x < 10; x++){
        struct Student* student = &students[x];
        printf("id = %d\tscore = %d\n", student->id, student->score);
        struct Student* idstudent = &idstudents[student->id];
        *idstudent = *student; /* copy all fields at once */
        printf("id = %d\tscore = %d\n", idstudent->id, idstudent->score);/* pointless here, since we just printed the same info via student */
    }

    for (int x = 0; x < 10; x++){
        struct Student* idstudent = &idstudents[x];
        printf("id = %d\tscore = %d\n", idstudent->id, idstudent->score);
    }
    deallocate(idstudents);
}
void输出(struct Student*students){
struct Student*idstudents=allocate();/*按id排序*/
如果(!idstudents)
/*句柄分配错误*/;
对于(int x=0;x<10;x++){
struct Student*Student=&students[x];
printf(“id=%d\t分数=%d\n”,学生->id,学生->分数);
结构Student*idstudent=&idstudent[Student->id];
*idstudent=*student;/*一次复制所有字段*/
printf(“id=%d\t核心=%d\n”,idstudent->id,idstudent->score);/*这里没有意义,因为我们刚刚通过student打印了相同的信息*/
}
对于(int x=0;x<10;x++){
结构Student*idstudent=&idstudents[x];
printf(“id=%d\t核心=%d\n”,idstudent->id,idstudent->score);
}
取消分配(idstudents);
}
这会改变
学生
点的位置,因此下次通过循环时,您将从那里开始偏移,而不是从一开始。也就是说,您将获得
原始学生[0]
原始学生[1]
原始学生[1+2]
原始学生[1+2+3]
,等等。您在
sd
方面也有同样的问题

相反,您希望使用不同的变量,例如

struct student* st = &students[x];
printf("id = %d\tscore = %d\n", st->id, st->score);
etc
还有,sd的用途是什么?你似乎无缘无故地分配了一些空间并将学生复制到sd。分配的空间i