gpt4 book ai didi

c - 使用指针在 C 中对结构进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:39 26 4
gpt4 key购买 nike

我刚开始使用 C,对幕后发生的事情一无所知。我正在为一个数据结构类即时学习它,这让事情变得有点困难。

更新:我已经剥离了程序,从内存开始,然后再往上。我在那里有分配和解除分配函数,但出现 malloc 错误:Q1(9882) malloc: * error for object 0x7fff59daec08: pointer being freed was not allocated*在malloc_error_break中设置断点调试

Update2 这是我修改后的代码,它仍然缺少一些东西,我的几个 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;
}

最佳答案

students = &students[x];

这会改变 students 指向的位置,因此下一次循环时您将从那里开始偏移,而不是从头开始。也就是说,您将获得 originalstudents[0]originalstudents[1]originalstudents[1+2]originalstudents[ 1+2+3] 等。sd 也有同样的问题。

相反,你想使用不同的变量,比如

struct student* st = &students[x];
printf("id = %d\tscore = %d\n", st->id, st->score);
etc

另外,sd 是做什么用的?您似乎无缘无故地分配了一些空间并将学生复制到 sd。分配的空间未保存或返回......这是内存泄漏。哦,等等,我明白了……你按照学生的 ID 对 sd 中的学生重新排序。所以你应该在完成后释放内存。但是对于学生和 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);
}

关于c - 使用指针在 C 中对结构进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12738327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com