我已經創建了一個結構數組,並且我想使用qsort對它們進行排序以按字母順序排序日期到字符串月份或我應該說char月份[]。我怎麼能使下面的代碼顯示一個月的結構。請指教。感謝如何在C編程中使用qsort對結構進行排序
struct dates
{
int index;
int day;
int year;
char month[15];
};
int i=0;
int count = 0 ;
char test ='\0';
int total =0;
printf("Please enter the number of dates you need to display");
scanf("%d",&total);
struct dates *ip[total];
for(count =0; count< total; count++){
ip[count] = (struct dates*)malloc(sizeof(struct dates));
printf("\nEnter the name month.");
scanf("%s", ip[count]->month);
printf("\nEnter the Day.");
scanf("%d",&ip[count]->day);
printf("\nEnter the Year.");
scanf("%d", &ip[count]->year);
}
for(i=0; i<total; i++){
printf("%s %d %d\n\n",ip[i]->month,ip[i]->day,ip[i]->year);
}
你有沒有看'人qsort'?文檔解釋了你需要做的事情,並舉例說明如何使用它。 – 2012-03-22 17:40:07