quick question on using bsearch() on an array of structs

Discussion in 'C Programming' started by Bit Byter, Apr 15, 2006.

  1. Bit Byter

    Bit Byter Guest

    I am hacking some legacy code and have put together a simple test to
    test some hashing funcs I've written. I now want to do a simplistic
    timing between the various structs.

    Here's a snippet:

    struct item_{
    char key[16];
    char data[32];
    };

    typedef struct {
    struct item_ items[NUM_ITEMS];
    }fakeHash ;


    void fooBar(){
    char buff[16];

    /* 1. generate random index */
    int rix =randix();

    /* 2. generate key based off of random index */
    sprintf(buff, "key%d",rix);

    /* search for key using bsearch (keys already sorted)
    bsearch((void*)&buff,(void*)fh.items.key,NUM_ITEMS,sizeof(struct
    item_.key), (void*)strcmp )
    */

    }


    Any suggestions on using bsearch with this struct?. Note the commented
    out code is only pseudo code. I KNOW it won't work
     
    Bit Byter, Apr 15, 2006
    #1
    1. Advertising

  2. Bit Byter

    tmp123 Guest

    Bit Byter wrote:
    > I am hacking some legacy code and have put together a simple test to
    > test some hashing funcs I've written. I now want to do a simplistic
    > timing between the various structs.
    >
    > Here's a snippet:
    >
    > struct item_{
    > char key[16];
    > char data[32];
    > };
    >
    > typedef struct {
    > struct item_ items[NUM_ITEMS];
    > }fakeHash ;
    >
    >
    > void fooBar(){
    > char buff[16];
    >
    > /* 1. generate random index */
    > int rix =randix();
    >
    > /* 2. generate key based off of random index */
    > sprintf(buff, "key%d",rix);
    >
    > /* search for key using bsearch (keys already sorted)
    > bsearch((void*)&buff,(void*)fh.items.key,NUM_ITEMS,sizeof(struct
    > item_.key), (void*)strcmp )
    > */
    >
    > }
    >
    >
    > Any suggestions on using bsearch with this struct?. Note the commented
    > out code is only pseudo code. I KNOW it won't work


    Something like:

    struct item_ *p;
    p=bsearch( buff, fakeHash.items, NUM_ITEMS, sizeof(struct item_),
    (void *)strcmp );
     
    tmp123, Apr 15, 2006
    #2
    1. Advertising

  3. On 15 Apr 2006 07:41:54 -0700, "tmp123" <> wrote:

    >Bit Byter wrote:
    >> I am hacking some legacy code and have put together a simple test to
    >> test some hashing funcs I've written. I now want to do a simplistic
    >> timing between the various structs.
    >>
    >> Here's a snippet:
    >>
    >> struct item_{
    >> char key[16];
    >> char data[32];
    >> };
    >>
    >> typedef struct {
    >> struct item_ items[NUM_ITEMS];
    >> }fakeHash ;
    >>
    >>
    >> void fooBar(){
    >> char buff[16];
    >>
    >> /* 1. generate random index */
    >> int rix =randix();
    >>
    >> /* 2. generate key based off of random index */
    >> sprintf(buff, "key%d",rix);
    >>
    >> /* search for key using bsearch (keys already sorted)
    >> bsearch((void*)&buff,(void*)fh.items.key,NUM_ITEMS,sizeof(struct
    >> item_.key), (void*)strcmp )
    >> */
    >>
    >> }
    >>
    >>
    >> Any suggestions on using bsearch with this struct?. Note the commented
    >> out code is only pseudo code. I KNOW it won't work

    >
    >Something like:
    >
    > struct item_ *p;
    > p=bsearch( buff, fakeHash.items, NUM_ITEMS, sizeof(struct item_),
    >(void *)strcmp );


    The cast for strcmp is incorrect and guarantees a diagnostic since
    there is no implied conversion between function and object pointers..
    The correct cast would be
    (int (*)(const void*,const void*))strcmp

    This only works because char* and void* are guaranteed to have the
    same representation. In general, it would be better to provide you
    own wrapper.


    Remove del for email
     
    Barry Schwarz, Apr 16, 2006
    #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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    641
    Al Bowers
    Apr 5, 2004
  2. Michiel Rapati-Kekkonen

    bsearch and structs, again

    Michiel Rapati-Kekkonen, Jul 7, 2004, in forum: C Programming
    Replies:
    2
    Views:
    776
    Michiel Rapati-Kekkonen
    Jul 8, 2004
  3. Paminu
    Replies:
    5
    Views:
    644
    Eric Sosman
    Oct 11, 2005
  4. Steph

    bsearch() with dynamic array

    Steph, Jun 23, 2007, in forum: C Programming
    Replies:
    5
    Views:
    450
    Barry Schwarz
    Jul 8, 2007
  5. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page