Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > qsort wiht struct and pointers

Reply
Thread Tools

qsort wiht struct and pointers

 
 
Maria Mela
Guest
Posts: n/a
 
      01-25-2007
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", aux[i]num_stu);
}

Kisses



 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-25-2007
"Maria Mela" <(E-Mail Removed)> writes:

> 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", aux[i]num_stu);
> }


And this is missing a ".".
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-25-2007
In article <newscache$lrufcj$osl$(E-Mail Removed)>,
Maria Mela <(E-Mail Removed)> wrote:
>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.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
 
Reply With Quote
 
Maria Mela
Guest
Posts: n/a
 
      01-25-2007
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" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> "Maria Mela" <(E-Mail Removed)> writes:
>
>> 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", aux[i]num_stu);
>> }

>
> And this is missing a ".".
> --
> "The expression isn't unclear *at all* and only an expert could actually
> have doubts about it"
> --Dan Pop



 
Reply With Quote
 
David T. Ashley
Guest
Posts: n/a
 
      01-25-2007
"Maria Mela" <(E-Mail Removed)> wrote in message
news:newscache$lrufcj$osl$(E-Mail Removed) ...
> 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", aux[i]num_stu);
> }


Specific error messages and line numbers, please.

--
David T. Ashley ((E-Mail Removed))
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)


 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      01-26-2007
Maria Mela wrote:

> 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.]

--
Chris "electric hedgehog" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      01-26-2007
On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <(E-Mail Removed)>
wrote:

>"Maria Mela" <(E-Mail Removed)> writes:
>
>> 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
--
jay
 
Reply With Quote
 
Maria Mela
Guest
Posts: n/a
 
      01-26-2007
Itīs better to define one 'MAXEntries' to my struct?
The qsort function works better it this definition?

"jaysome" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <(E-Mail Removed)>
> wrote:
>
>>"Maria Mela" <(E-Mail Removed)> writes:
>>
>>> 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
> --
> jay



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-26-2007
Maria Mela wrote:

> It4s better to define one 'MAXEntries' to my struct?


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-27-2007
In article <(E-Mail Removed)>,
Default Loser <(E-Mail Removed)> wrote:
....
>Please don't top-post. Your replies belong following or interspersed
>with properly trimmed quotes. See the majority of other posts in the
>newsgroup, or:
><http://www.caliburn.nl/topposting.html>


Get a life!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems wiht nant Ireneus Broncel Java 3 10-21-2005 03:17 PM
Questions about qsort( ) to sort pointer to struct. PCHOME C Programming 4 07-08-2005 08:56 PM
Calculating subnet wiht netmask... sam C++ 1 05-11-2005 05:29 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Microphone does not work wiht XP Kevin Buthorn Computer Support 5 12-28-2003 04:33 PM



Advertisments