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(co nst 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 10 5639
"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(co nst 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(co nst 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
In article <ne************ ********@newsfr ont4.netvisao.p t>,
Maria Mela <h4***@netvisao .ptwrote:
>What´s wrong in my code?? I can´t compile and sort my struct data...
>int sortbynumber(co nst 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(co nst 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_entri es, 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
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.stanfor d.eduescreveu na mensagem
news:87******** ****@blp.benpfa ff.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(co nst 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(co nst 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\Sor t 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
"Maria Mela" <h4***@netvisao .ptwrote in message
news:ne******** ************@ne wsfront4.netvis ao.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(co nst 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)
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/
On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <bl*@cs.stanfor d.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(co nst 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(c onst 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(co nst 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
It´s better to define one 'MAXEntries' to my struct?
The qsort function works better it this definition?
"jaysome" <ja*****@hotmai l.comescreveu na mensagem
news:2d******** *************** *********@4ax.c om...
On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <bl*@cs.stanfor d.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(co nst 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(co nst 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
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 >
In article <51************ *@mid.individua l.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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 and the program dies in the midst of the
qsort.
My comparison function doesn't do anything fancy - just some
strnicmp's and =='s.
|
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) becomes 11,
then the program crashes. It is obviously qsort that may me
overwritten the used memory location. The weird thing is that it is
ALWAYS the fourth time running the program. Below is the code
MEMORY LOCATION OK HERE
for (i = 0; i...
|
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
{
|
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.
|
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
| |
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;
|
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
|
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 around forming the array of pointers to structs but I can't seem to get it to qsort at all. I have looked at some other code that I have found but I can't seem to figure it out.
I need help with the details of the "compare_keyword" function, that is...
|
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 with this:
void qsort(int v, int left, int right)
{
int i, last;
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |