473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

qsort wiht struct and pointers

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

Jan 25 '07 #1
10 5622
"Maria Mela" <h4***@netvisao.ptwrites:
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
Jan 25 '07 #2
In article <ne********************@newsfront4.netvisao.pt>,
Maria Mela <h4***@netvisao.ptwrote:
>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
Jan 25 '07 #3
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" <bl*@cs.stanford.eduescreveu na mensagem
news:87************@blp.benpfaff.org...
"Maria Mela" <h4***@netvisao.ptwrites:
>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

Jan 25 '07 #4
"Maria Mela" <h4***@netvisao.ptwrote in message
news:ne********************@newsfront4.netvisao.pt ...
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 (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 25 '07 #5
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/

Jan 26 '07 #6
On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>"Maria Mela" <h4***@netvisao.ptwrites:
>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
Jan 26 '07 #7
Itīs better to define one 'MAXEntries' to my struct?
The qsort function works better it this definition?

"jaysome" <ja*****@hotmail.comescreveu na mensagem
news:2d********************************@4ax.com...
On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>>"Maria Mela" <h4***@netvisao.ptwrites:
>>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

Jan 26 '07 #8
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>
Jan 26 '07 #9
In article <51*************@mid.individual.net>,
Default Loser <de***********@yahoo.comwrote:
....
>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!

Jan 27 '07 #10
On Thu, 25 Jan 2007 19:38:50 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
<snip>
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
Feb 6 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: richard | last post by:
What might cause qsort to crash? I'm using qsort to sort a few thousand items of a not too complex structure. Tried it with one data set - worked fine. Tried another similarly sized data set...
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
7
by: Angus Comber | last post by:
Hello Here is my code so far. Is this correct/incorrect/along the right lines/other? #include <stdio.h> #include <string.h> #include <search.h> struct mystruct {
5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
4
by: davidcollins001 | last post by:
Hi, I am trying to get more to grips with pointers in C so I am trying to make a program that reads a file, places the data in a struct then sorts it using qsort. I have just about got my head...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.