qsort wiht struct and pointers

M

Maria Mela

Hello Everyone,

What´s wrong in my code?? I can´t compile and sort my struct data...

Here´s peace of my code...


typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

void Insert_Student (PtrStudent *lista);
PtrStudent apStudent =NULL;
****************
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);
printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){
printf("%d", auxnum_stu);
}

Kisses
 
B

Ben Pfaff

Maria Mela said:
typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

I don't recommend creating typedefs, in general, but typedefs for
pointer types are particularly confusing.
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}

This comparison function is prone to overflow and it doesn't have
the right parameter types. Here's a better version:

int
sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
if (c1->num_stu > c2->num_stu)
return 1;
else if (c1->num_stu < c2->num_stu)
return -1;
else
return 0;
}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

There are many things wrong with this. The first argument should
be a pointer to the beginning of an array, but you didn't mention
what "aux" is. The second argument should be the number of
elements in the array, but you're trying to pass a type. The
third argument seems to be correct. The fourth argument does not
need the cast once you've fixed the sortbynumber function.
printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){

Again, you declared PtrStudent as a typedef. Do you understand
what a typedef is? You can't compare a variable to a type.
printf("%d", auxnum_stu);
}


And this is missing a ".".
 
W

Walter Roberson

What´s wrong in my code?? I can´t compile and sort my struct data...
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);
}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

When you call qsort, you are taking the -function- pointer sortbynumber
and trying to force it to be a generic pointer to an -object-,
by casting it to (void *) . Casting function pointers to objects
is not defined by standard C -- implementations can allow it
if they want, but implementations are not required to allow it at all.

The third argument to qsort() should not be a (void *) : it
should be int (*compar) (const void *, const void *)
which is a pointer to a function that takes two const void* arguments and
returns an int. That is -almost- the same as your definition of
sortbynumber . If you were to define sortbynumber as

int sortbynumber(const void *c1, const void *c2)
{
return (const Student *)c1->num_stu -
(const Student *)c2->num_stu;
}

then sortbynumber would already have the proper type for qsort
and you would be able to call

qsort( (void *) aux, (size_t) number_of_entries, sizeof (Student),
sortbynumber );

Note that you had PtrStudent as your second argument to qsort(),
but PtrStudent is just a type name, and what you need to pass
in the second argument is the number of entries in the table.
 
M

Maria Mela

Thks for your answear...
the "aux" in my code...

void Insert_Student (PtrStudent *lista)
{
PtrStudent aux=NULL, start=NULL;
aux=(PtrAluno) malloc (sizeof(Aluno));

In you r opinion, how can i put this expression?
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

Kisses

Ben Pfaff said:
Maria Mela said:
typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

I don't recommend creating typedefs, in general, but typedefs for
pointer types are particularly confusing.
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}

This comparison function is prone to overflow and it doesn't have
the right parameter types. Here's a better version:

int
sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
if (c1->num_stu > c2->num_stu)
return 1;
else if (c1->num_stu < c2->num_stu)
return -1;
else
return 0;
}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

There are many things wrong with this. The first argument should
be a pointer to the beginning of an array, but you didn't mention
what "aux" is. The second argument should be the number of
elements in the array, but you're trying to pass a type. The
third argument seems to be correct. The fourth argument does not
need the cast once you've fixed the sortbynumber function.
printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){

Again, you declared PtrStudent as a typedef. Do you understand
what a typedef is? You can't compare a variable to a type.
printf("%d", auxnum_stu);
}


And this is missing a ".".
 
D

David T. Ashley

Maria Mela said:
Hello Everyone,

What´s wrong in my code?? I can´t compile and sort my struct data...

Here´s peace of my code...

typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

void Insert_Student (PtrStudent *lista);
PtrStudent apStudent =NULL;
****************
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);
printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){
printf("%d", auxnum_stu);
}


Specific error messages and line numbers, please.
 
C

Chris Dollin

Maria said:
Thks for your answear...
the "aux" in my code...

void Insert_Student (PtrStudent *lista)
{
PtrStudent aux=NULL, start=NULL;
aux=(PtrAluno) malloc (sizeof(Aluno));

I know the Initialisation War is elsewhere, but really, declaring
a variable as null and then /immediately/ assigning its proper
value to it seems ... crazy.

PtrStudent aux = malloc( sizeof (*aux) );
PtrStudent start = NULL;

[I've "fixed" the arguemnt to `malloc` to the Usual Form that
doesn't depend on knowing what type `aux` is supposed to point to.]
 
J

jaysome

Maria Mela said:
typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

I don't recommend creating typedefs, in general, but typedefs for
pointer types are particularly confusing.
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}

This comparison function is prone to overflow and it doesn't have
the right parameter types. Here's a better version:

int
sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
if (c1->num_stu > c2->num_stu)
return 1;
else if (c1->num_stu < c2->num_stu)
return -1;
else
return 0;
}

And another version, using the Lawrence Kirby "cutie":

int sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
return (c1->num_stu < c2->num_stu) ? -1 :
(c1->num_stu > c2->num_stu);
}

Regards
 
M

Maria Mela

It´s better to define one 'MAXEntries' to my struct?
The qsort function works better it this definition?

jaysome said:
Maria Mela said:
typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

I don't recommend creating typedefs, in general, but typedefs for
pointer types are particularly confusing.
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}

This comparison function is prone to overflow and it doesn't have
the right parameter types. Here's a better version:

int
sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
if (c1->num_stu > c2->num_stu)
return 1;
else if (c1->num_stu < c2->num_stu)
return -1;
else
return 0;
}

And another version, using the Lawrence Kirby "cutie":

int sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
return (c1->num_stu < c2->num_stu) ? -1 :
(c1->num_stu > c2->num_stu);
}

Regards
 
D

Dave Thompson

The third argument to qsort() should not be a (void *) : it
should be int (*compar) (const void *, const void *)
which is a pointer to a function that takes two const void* arguments and
Right.

returns an int. That is -almost- the same as your definition of
sortbynumber . If you were to define sortbynumber as

int sortbynumber(const void *c1, const void *c2)
{
return (const Student *)c1->num_stu -
(const Student *)c2->num_stu;

( (const Student *)c1 ) -> num_stu and similarly for c2
}

then sortbynumber would already have the proper type for qsort
and you would be able to call

qsort( (void *) aux, (size_t) number_of_entries, sizeof (Student),
sortbynumber );
Although you wouldn't need either of those casts. Unless aux is
declared as a pointer to const something but is actually pointing to a
nonconst something so the sort call is valid, which would usually be
unwise and probably silly. If aux is actually a pointer to Student, as
it probably should be, you could use sizeof *ptr .
Note that you had PtrStudent as your second argument to qsort(),
but PtrStudent is just a type name, and what you need to pass
in the second argument is the number of entries in the table.

Right.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top