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

B

Bit Byter

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
 
T

tmp123

Bit said:
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 );
 
B

Barry Schwarz

Bit said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top