Re: qsort and structs and ptrs

Discussion in 'C Programming' started by richard, Aug 14, 2003.

  1. richard

    richard Guest

    aha! why is it that posting focuses the mind?

    in cmp:

    const Item *sp1 = *(Item * const *)p1;

    in main:

    qsort((void *)(list.item), list.entries, sizeof(Item*), cmp);

    seems to work



    On Wed, 13 Aug 2003 22:09:31 GMT, richard <> wrote:

    >I'd like to qsort part of a structure.
    >
    >typedef struct {
    > char *this;
    > char *that;
    >} Item
    >
    >typedef struct {
    > int entries;
    > Item **items;
    >} List
    >---------
    >List list;
    >qsort((void *)&(list.items), list.entries, sizeof(Item*), cmp);
    >------------
    >int cmp(const void *p1, const void *p2) {
    > const struct Item **sp1 = p1;
    > const struct Item **sp2 = p1;
    >
    > return strcmp(sp1->this, sp2->this);
    >}
    >--------------------------
    >My compiler doesn't like the sp1=p1 line or the strcmp line or both.
    >I've tried a lot of variations on the cmp function (and the qsort
    >invocation), but nothing appears to work. Any suggestions would be
    >appreciated.
    >
    >thanks
    >
    richard, Aug 14, 2003
    #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. Thomas Matthews

    Ptrs to member funcs & inheritance

    Thomas Matthews, Dec 20, 2003, in forum: C++
    Replies:
    2
    Views:
    381
    David White
    Dec 20, 2003
  2. B. Deepak
    Replies:
    1
    Views:
    512
    Kerem OZMAN
    May 23, 2006
  3. LBJ

    using volatile ptrs ...

    LBJ, Nov 10, 2004, in forum: C Programming
    Replies:
    6
    Views:
    404
    Lawrence Kirby
    Nov 12, 2004
  4. Replies:
    3
    Views:
    494
    Steven Kobes
    Jul 27, 2005
  5. Bidule

    qsort of malloc'ed structs

    Bidule, Jan 29, 2006, in forum: C Programming
    Replies:
    5
    Views:
    391
    Bidule
    Jan 30, 2006
Loading...

Share This Page