程序崩溃时使用qsort()排序结构指针数组

时间:2016-06-16 17:19:21

标签: c structure qsort

下面是我尝试使用int avg排序的程序。我正在对compare()进行排序。它会在排序和崩溃后打印垃圾值。我确信#include<stdio.h> #include<stdlib.h> #define LIMIT 3 int compare(const void *, const void *); struct player { char name[10]; int age; int n_o_t; int avg; }; int compare(const void *a,const void *b) { struct player *A=a; struct player *B=b; return(B->avg - A->avg); } int main() { struct player * game[LIMIT]; int i=0; for(i=0;i<LIMIT;i++) { game[i]=malloc(sizeof(struct player)); if(game[i]) { printf("Enter details\n"); fflush(stdin); //fgets(game[i]->name,9,stdin); gets(game[i]->name); printf("age not ave\n"); scanf("%d %d %d",&game[i]->age,&game[i]->n_o_t,&game[i]->avg); fflush(stdin); } else { printf("unable to allocate memory\n"); } } for(i=0;i<LIMIT;i++) { printf("%s %d %d %d \n",game[i]->name,game[i]->age,game[i]->n_o_t,game[i]->avg); } qsort(game,LIMIT,sizeof(struct player),compare); printf("\nNow the sorted struct is\n\n"); for(i=0;i<LIMIT;i++) { printf("%s %d %d %d \n",game[i]->name,game[i]->age,game[i]->n_o_t,game[i]->avg); } return 0; } 有些不对劲但不确定它是什么。有人可以帮忙。

#include<stdio.h>
#include<stdlib.h>
#define LIMIT 5
int compare(const void *, const void *);

struct player
{
char name[10];
int age;
int n_o_t;
int avg;
};

int compare(const void *a,const void *b)
{
struct player *A=a;
struct player *B=b;
return(B->avg - A->avg);
}

int main()
{
struct player * game[LIMIT];
int i=0;

for(i=0;i<LIMIT;i++)
{
    game[i]=malloc(sizeof(struct player));
    if(game[i])
    {
        printf("Enter details\n");
        fflush(stdin);
        fgets(game[i]->name,9,stdin);
        game[i]->name[strlen(game[i]->name)-1]='\0'; // to remove trailing newline char
        printf("age n_o_t ave\n");
        scanf("%d %d %d",&game[i]->age,&game[i]->n_o_t,&game[i]->avg);
    }
    else
    {
        printf("unable to allocate memory\n");
    }
}

for(i=0;i<LIMIT;i++)
{
    printf("%s %d %d %d \n",game[i]->name,game[i]->age,game[i]->n_o_t,game[i]->avg);
}

qsort(game,LIMIT,sizeof(struct player *),compare);

printf("\nNow the sorted struct is\n\n");

for(i=0;i<LIMIT;i++)
{
    printf("%s %d %d %d \n",game[i]->name,game[i]->age,game[i]->n_o_t,game[i]->avg);
}

return 0;

}

我已在下面的代码中进行了必要的更改。现在它运行良好但没有对数组进行排序。

n1 11 12 **15**
n2 16 18 **19**
n3 22 25 **0**
n4 77 66 **88**
n5 3 2 **1**

如果输入是

n1 11 12 **15** n2 16 18 **19** n4 77 66 **88** n3 22 25 **0** n5 3 2 **1**

输出为

{{1}}

上面的矩阵应该按照最后一位数

进行排序

2 个答案:

答案 0 :(得分:0)

下面:

struct player *A=*((struct player**)a);
struct player *B=*((struct player**)b);

另外,这里:

game[i]=(struct player*)malloc(sizeof(struct player));

这些是显式指针转换。另外,在这里:

qsort(game,LIMIT,sizeof(struct player*),compare);

由于game数组由struct player*类型组成。

另外,我建议使用getline()代替gets()fflush()。我想,休息很好。

答案 1 :(得分:0)

因为你有一个指针列表,qsort会给出一个指向指针的指针。此外,您的fscan可能需要一些帮助

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIMIT 5
int compare(const void *, const void *);

struct player
{
  char name[10];
  int age;
  int n_o_t;
  int avg;
};

int compare(const void *a,const void *b)
{
  struct player *A=*(struct player **)a; /* note the double ** */
  struct player *B=*(struct player **)b; /* note the double ** */
  return(B->avg - A->avg);
}

int main()
{
  struct player * game[LIMIT];
  int i=0;

  for(i=0;i<LIMIT;i++)
  {
    game[i]=malloc(sizeof(struct player));
    if(game[i])
    {
      printf("Enter details\n");
      fflush(stdin);
      fgets(game[i]->name,9,stdin);
      game[i]->name[strlen(game[i]->name)-1]='\0'; // to remove trailing newline char
      printf("age n_o_t ave\n");
      scanf("%d %d %d",&game[i]->age,&game[i]->n_o_t,&game[i]->avg);
    }
    else
    {
        printf("unable to allocate memory\n");
    }
  }

  for(i=0;i<LIMIT;i++)
  {
    printf("%s %d %d %d \n",game[i]->name,game[i]->age,game[i]->n_o_t,game[i]->avg);
  }

  qsort(game,LIMIT,sizeof(struct player *),compare);

  printf("\nNow the sorted struct is\n\n");

  for(i=0;i<LIMIT;i++)
  {
    printf("%s %d %d %d \n",game[i]->name,game[i]->age,game[i]->n_o_t,game[i]->avg);
  }

  return 0;
}