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

Program Crashing while sorting array of structure pointers using qsort()

您好,下面是我尝试使用 qsort() 排序的程序。我正在根据 int avg 对其进行排序。它打印垃圾值 post 排序和崩溃。我确定 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]='[=11=]'; // 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**

上面的矩阵应该已经根据最后一位进行了排序

这里:

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()。休息,我想,没关系。

因为你有一个指针列表,所以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]='[=10=]'; // 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;
}