470,833 Members | 1,401 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

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

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
2 2912
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
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.

Similar topics

3 posts views Thread by Sourin | last post: by
1 post views Thread by Ramprasad A Padmanabhan | last post: by
11 posts views Thread by Ramprasad A Padmanabhan | last post: by
6 posts views Thread by Michiel Rapati-Kekkonen | last post: by
14 posts views Thread by Roman Mashak | last post: by
4 posts views Thread by Davy | last post: by
reply views Thread by jyotsnamk | last post: by
4 posts views Thread by Steph | last post: by
4 posts views Thread by Amandil | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.