qsort wiht struct and pointers

Discussion in 'C Programming' started by Maria Mela, Jan 25, 2007.

  1. Maria Mela

    Maria Mela Guest

    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
    Maria Mela, Jan 25, 2007
    #1
    1. Advertising

  2. Maria Mela

    Ben Pfaff Guest

    "Maria Mela" <> 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", auxnum_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
    Ben Pfaff, Jan 25, 2007
    #2
    1. Advertising

  3. In article <newscache$lrufcj$osl$>,
    Maria Mela <> 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
    Walter Roberson, Jan 25, 2007
    #3
  4. Maria Mela

    Maria Mela Guest

    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" <> escreveu na mensagem
    news:...
    > "Maria Mela" <> 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", auxnum_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, Jan 25, 2007
    #4
  5. "Maria Mela" <> wrote in message
    news:newscache$lrufcj$osl$...
    > 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.

    --
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
    David T. Ashley, Jan 25, 2007
    #5
  6. Maria Mela

    Chris Dollin Guest

    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/
    Chris Dollin, Jan 26, 2007
    #6
  7. Maria Mela

    jaysome Guest

    On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <>
    wrote:

    >"Maria Mela" <> 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
    jaysome, Jan 26, 2007
    #7
  8. Maria Mela

    Maria Mela Guest

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

    "jaysome" <> escreveu na mensagem
    news:...
    > On Thu, 25 Jan 2007 11:21:19 -0800, Ben Pfaff <>
    > wrote:
    >
    >>"Maria Mela" <> 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
    Maria Mela, Jan 26, 2007
    #8
  9. Maria Mela

    Default User Guest

    Re: qsort wiht struct and pointers - TPA

    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>
    Default User, Jan 26, 2007
    #9
  10. Re: qsort wiht struct and pointers - GAL

    In article <>,
    Default Loser <> 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!
    Kenny McCormack, Jan 27, 2007
    #10
  11. On Thu, 25 Jan 2007 19:38:50 +0000 (UTC), -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
    Dave Thompson, Feb 6, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ireneus Broncel

    Problems wiht nant

    Ireneus Broncel, Oct 21, 2005, in forum: Java
    Replies:
    3
    Views:
    555
    Bryce
    Oct 21, 2005
  2. sam
    Replies:
    1
    Views:
    2,658
    Victor Bazarov
    May 11, 2005
  3. Fernando

    Usenet posting wiht python

    Fernando, Sep 12, 2003, in forum: Python
    Replies:
    2
    Views:
    854
    Peter Scott
    Sep 12, 2003
  4. MG
    Replies:
    1
    Views:
    337
    =?Utf-8?B?U2l2YSBN?=
    Jul 20, 2007
  5. Gordon
    Replies:
    0
    Views:
    91
    Gordon
    Nov 25, 2003
Loading...

Share This Page