sort of list of pointers

Discussion in 'C Programming' started by MisterE, Sep 26, 2008.

  1. MisterE

    MisterE Guest

    // Is any of this not portable correct etc?
    #include <stdlib.h>

    typedef struct b
    {
    int f;
    } B;

    //Should pointers to pointers be avoided or nothing wrong with this:
    int compare( const void *arg1, const void *arg2 )
    {
    B **b1;
    B **b2;
    b1 = (B **)arg1;
    b2 = (B **)arg2;
    //And are there any reasons to use 1 of these lines over the other:
    return ((**b1).f - (**b2).f);
    //return (*b1)->f - (*b2)->f;
    }

    int main (int argc, char *argv[])
    {
    B **list;
    int j = 100;
    list = malloc(sizeof(B *)*j);
    if (list == NULL) return -1;
    while(j)
    {
    j--;
    list[j] = malloc(sizeof(B));
    if (list[j] == NULL) return -2;
    list[j]->f = j;
    }
    qsort(list,100,sizeof(B *),compare);
    return 0;
    }
     
    MisterE, Sep 26, 2008
    #1
    1. Advertising

  2. On Sat, 27 Sep 2008 08:48:44 +1000, MisterE <>
    wrote:

    >// Is any of this not portable correct etc?


    // comments are not portable unless you are restricting yourself to
    C99 systems.

    >#include <stdlib.h>
    >
    >typedef struct b
    >{
    > int f;
    >} B;
    >
    >//Should pointers to pointers be avoided or nothing wrong with this:


    When sorting pointers it is almost impossible to avoid them.

    Why should any feature of the language be avoided?

    >int compare( const void *arg1, const void *arg2 )
    >{
    > B **b1;
    > B **b2;
    > b1 = (B **)arg1;
    > b2 = (B **)arg2;
    > //And are there any reasons to use 1 of these lines over the other:
    > return ((**b1).f - (**b2).f);
    > //return (*b1)->f - (*b2)->f;


    Either can overflow. You might want to consider using comparisons
    (possibly with the conditional operator).

    >}
    >
    >int main (int argc, char *argv[])
    >{
    > B **list;
    > int j = 100;
    > list = malloc(sizeof(B *)*j);
    > if (list == NULL) return -1;


    EXIT_FAILURE, EXIT_SUCCESS, and 0 are the only portable return values
    from main.

    > while(j)
    > {
    > j--;
    > list[j] = malloc(sizeof(B));
    > if (list[j] == NULL) return -2;
    > list[j]->f = j;
    > }
    > qsort(list,100,sizeof(B *),compare);
    > return 0;
    >}
    >
    >


    --
    Remove del for email
     
    Barry Schwarz, Sep 27, 2008
    #2
    1. Advertising

  3. MisterE

    Ian Collins Guest

    pete wrote:
    >
    > /* BEGIN new.c */
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > #define J_INIT 100
    >
    > typedef struct b {
    > int f;
    > } B;
    >
    > int compare( const void *arg1, const void *arg2 )
    > {
    > B **b1 = (B **)arg1;
    > B **b2 = (B **)arg2;
    >
    > return (*b2) -> f > (*b1) -> f ? -1
    > : (*b2) -> f != (*b1) -> f;


    I do hate comparisons where one has to ponder what's going on. Why not
    something like

    int compare( const void *arg1, const void *arg2 )
    {
    /* Do the messy stuff in one place.
    */
    int lhs = (*(const B**)(arg1))->f;
    int rhs = (*(const B**)(arg2))->f;

    return lhs == rhs ? 0 : lhs > rhs ? 1 : -1;
    }

    or even;

    if (i > j) return 1;
    if (i < j) return -1;
    return (0);

    --
    Ian Collins.
     
    Ian Collins, Sep 27, 2008
    #3
    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. Tim Partridge

    list::sort with pointers

    Tim Partridge, Aug 13, 2003, in forum: C++
    Replies:
    2
    Views:
    370
    John Harrison
    Aug 13, 2003
  2. Sean
    Replies:
    2
    Views:
    653
    loufoque
    Sep 24, 2006
  3. Replies:
    7
    Views:
    764
    Stefan Arentz
    Sep 10, 2007
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    711
  5. Navin
    Replies:
    1
    Views:
    744
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page