Search code examples
cstructureqsort

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


Hi below is the program that I am trying to sort using qsort(). I am sorting it with respect to int avg. It prints junk values post sorting and crashes. I am certain there is something wrong in compare() but not sure what it is. Can someone please help.

#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;

}

I have made the required changes in the code below. Now it's running fine yet not sorting the array.

#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;

}

If the input is

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

The output would be

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

The above matrix should have got sorted with respect to the last digit


Solution

  • since you have a list of pointers, qsort will give a pointer to the pointer. Also, your fscan probably needs some help

    #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;
    }