By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Apr 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 );

Apr 15 '06 #2

P: n/a
On 15 Apr 2006 07:41:54 -0700, "tmp123" <tm****@menta.net> 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
Apr 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.