[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

qsort

Hi! i dont know nearly nothing about qsort..can you explain it some and find my errors in the program that sort the structures depend on the names of students?



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int stucmp(const Ogrenci *s1, const Ogrenci *);

int main(){

      int i;
      
typedef struct ogrenci{

            const char name[15];
            const char surname[15];
            const int id;

      }Ogrenci;

      Ogrenci bilgi[4];

      for(i=0;i<=3;i++){
      
      printf("Ad&#305;n&#305; gir: ");
      
      scanf("%s",bilgi[i].name);

      printf("Soyadini gir: ");

      scanf("%s",bilgi[i].surname);

      printf("Nosunu gir: ");

      scanf("%d",&bilgi[i].id);

      }


qsort(bilgi,4,sizeof(Ogrenci),stucmp);

for(i=0;i<=3;i++)
printf("\n%s  %s   %d  \n",bilgi[i].name,bilgi[i].surname,bilgi[i].id);

return 0;
}


int stucmp(const Ogrenci *s1, const Ogrenci *)
{
return strcmp(s1->name,s2->name);
}
0
btocakci
Asked:
btocakci
  • 2
  • 2
2 Solutions
 
btocakciAuthor Commented:
THESE ARE THE ERRORS:
error C2143: syntax error : missing ',' before '*'
error C2059: syntax error : '*'
 error C2512: 'ogrenci' : no appropriate default constructor available
error C2664: 'qsort' : cannot convert parameter 4 from 'int (const int)' to 'int (__cdecl *)(const void *,const void *)'
        None of the functions with this name in scope match the target type
 error C2143: syntax error : missing ',' before '*'
error C2059: syntax error : '*'
error C2065: 's1' : undeclared identifier
 error C2227: left of '->name' must point to class/struct/union
 error C2065: 's2' : undeclared identifier
Cerror C2227: left of '->nsme' must point to class/struct/union
0
 
jkrCommented:
Try

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct ogrenci{

          const char name[15];
          const char surname[15];
          const int id;

     }Ogrenci;


int stucmp(const Ogrenci *s1, const Ogrenci *s2);

int main(){

     int i;
     

     Ogrenci bilgi[4];

     for(i=0;i<=3;i++){
     
     printf("Ad&#305;n&#305; gir: ");
     
     scanf("%s",bilgi[i].name);

     printf("Soyadini gir: ");

     scanf("%s",bilgi[i].surname);

     printf("Nosunu gir: ");

     scanf("%d",&bilgi[i].id);

     }


qsort(bilgi,4,sizeof(Ogrenci),stucmp);

for(i=0;i<=3;i++)
printf("\n%s  %s   %d  \n",bilgi[i].name,bilgi[i].surname,bilgi[i].id);

return 0;
}


int stucmp(const Ogrenci *s1, const Ogrenci *s2)
{
return strcmp(s1->name,s2->name);
}

You need to declare the struct outside of 'main()' and to name the parameters in your sorting function.
0
 
brettmjohnsonCommented:
> error C2512: 'ogrenci' : no appropriate default constructor available

This is the prime indicator that the compiler thinks it is compiling C++ code rather than ANSI C code.
That explains about half of the errors.  Make sure the source code filename extension is .c (lower case c) and that you are invoking the C front-end rather than the C++ front-end to the compiler.


> typedef struct ogrenci{

You need to move the definition of the structure outside the scope of main().  In fact, you should move it to before the stucmp() function prototype.

0
 
btocakciAuthor Commented:
thanx..

 error C2223: left of '->name' must point to struct/union

why i didnt manage to declare that the argument stucmp takes(*s1) is a struct, how can I manage this?
0
 
jkrCommented:
Try the corrected code I posted. It compiles without any errors.

>>why i didnt manage to declare that the argument stucmp takes(*s1) is a struct, how can I manage
>>this?

All you need to do is to name the parameters if you want to use them inside the function. Again, the struct must be declared globally.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now