problem with qsort - Visual Studio

Discussion in 'C Programming' started by Igal, May 21, 2008.

  1. Igal

    Igal Guest

    hay, i'm having problem with my qsort function.
    problem is with the sort, this sorts the array, but in wrong order,
    when i go to debug and check value of
    stu1->department or stu2->department
    i see that it points ot garbage and not to the strings it should point
    to.
    i just can't figure out what's wrong.

    typedef struct
    {
    unsigned long id;
    char full_name[LEN];
    char department[LEN];

    struct
    {
    unsigned sem_a;
    unsigned sem_b;
    }Grades;
    }Student;

    ....

    Student **sptr = NULL; //this is array of pointers to Student (it's
    not null when sort)

    ....

    qsort(sptr, ARR_SIZE, sizeof(Student*), SortDepName);

    ....

    int SortDepName(const void *e1, const void *e2)
    {
    Student *stu1 = (Student*)e1;
    Student *stu2 = (Student*)e2;

    if(strcmp(stu1->department, stu2->department) == 0)
    { return strcmp(stu1->full_name, stu2->full_name); }

    else { return strcmp(stu1->department, stu2->department); }
    }
     
    Igal, May 21, 2008
    #1
    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. wh
    Replies:
    2
    Views:
    540
    Cowboy \(Gregory A. Beamer\)
    Jan 16, 2004
  2. Thirumalai
    Replies:
    0
    Views:
    692
    Thirumalai
    May 22, 2006
  3. rockdale
    Replies:
    1
    Views:
    626
    Juan T. Llibre
    Aug 23, 2006
  4. xman
    Replies:
    0
    Views:
    527
  5. xman
    Replies:
    1
    Views:
    623
    Victor Bazarov
    Aug 17, 2005
Loading...

Share This Page