#include<stdio.h> #include<stdlib.h> #define maxsize 50 typedef struct { char name[maxsize]; int score; int rank; }student; student* initStu(int num) { student* s = (student*)malloc((sizeof(student)) * num); return s; } int main() { student* std; int temp, i, j; std = initStu(8); std[0] = { "a",81,0}; std[1] = { "b",88 ,0}; std[2] = { "c",66 ,0}; std[3] = { "d",25 ,0}; std[4] = { "f",31 ,0}; std[5] = { "g",91,0 }; std[6] = { "h",61 ,0}; std[7] = { "i",77,0 }; for (i = 0; i < 8; i++) { for (j = 0; j < 8-i; j++) { if (std[j].score > std[j+1].score) { temp = std[j].score; std[j].score = std[j + 1].score; std[j + 1].score = temp; } } } for (int i = 0; i < 8; i++) { printf("%d\n",std[i].score); } free(std); return 0; }哪出错了
时间: 2024-03-25 14:36:59 浏览: 16
这段代码存在两个问题:
1. 在结构体数组初始化时,不能使用类似 { } 的方式给结构体成员赋值,应该使用逐个赋值的方式。例如:
```
std[0].name = "a";
std[0].score = 81;
std[0].rank = 0;
```
2. 冒泡排序时,内层循环的终止条件应该是 `j < 8-i-1`,而不是 `j < 8-i`。因为每一轮内层循环都会将当前最大的数移到数组末尾,所以外层循环后面的元素已经是有序的,不需要再比较。
修改后的代码如下:
```
#include<stdio.h>
#include<stdlib.h>
#define maxsize 50
typedef struct {
char name[maxsize];
int score;
int rank;
} student;
student* initStu(int num) {
student* s = (student*)malloc((sizeof(student)) * num);
return s;
}
int main() {
student* std;
int temp, i, j;
std = initStu(8);
std[0].name = "a";
std[0].score = 81;
std[0].rank = 0;
std[1].name = "b";
std[1].score = 88;
std[1].rank = 0;
std[2].name = "c";
std[2].score = 66;
std[2].rank = 0;
std[3].name = "d";
std[3].score = 25;
std[3].rank = 0;
std[4].name = "f";
std[4].score = 31;
std[4].rank = 0;
std[5].name = "g";
std[5].score = 91;
std[5].rank = 0;
std[6].name = "h";
std[6].score = 61;
std[6].rank = 0;
std[7].name = "i";
std[7].score = 77;
std[7].rank = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8-i-1; j++) {
if (std[j].score > std[j+1].score) {
temp = std[j].score;
std[j].score = std[j + 1].score;
std[j + 1].score = temp;
}
}
}
for (int i = 0; i < 8; i++) {
printf("%d\n",std[i].score);
}
free(std);
return 0;
}
```
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)