有关结构体数组元素的排序求Debug
想用冒泡排序和指针给结构体内的整形进行排序 可是输出的结果不对
虚心学习
程序代码:
#include<stdio.h> #include<stdlib.h> #define STD 5 struct student{ int id; char name[20]; int text_id; int score[3]; }; int main() { int i,j; struct student std[STD] = { {1,"Mike",130210,{99,85,76}}, {2,"John",130211,{98,83,78}}, {3,"Tree",130212,{93,86,70}}, {4,"Jack",130213,{91,81,72}}, {5,"Nick",130214,{98,89,71}}, }; struct student * pointers[STD]; for(i = 0;i < STD;i++) { pointers[i] = &std[i]; } for(i = 0;i < STD - 1;i++) { for(j = 0;j < STD - i - 1;j++) { if(pointers[j] -> score[0] > pointers[j + 1] -> score[0]) { pointers[j] = &std[j + 1]; pointers[j + 1] = &std[j]; } } } for(i = 0;i < STD;i++) { printf("%d\n",pointers[i]->score[0]); } return 0; }