繁体   English   中英

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

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

您好,下面是我尝试使用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**

上面的矩阵应该相对于最后一位排序

这里:

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]='\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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM