473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 3072
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
1
by: Ramprasad A Padmanabhan | last post by:
I have written a simple script to search a word in an array But bsearch does not seem to work here. I know I am missing out something very simple , But I am not able to find out what Thanks...
11
by: Ramprasad A Padmanabhan | last post by:
I have got a pretty simple script , that uses bsearch to look for a particular element The problem is , it simply segfaults inside the compare function. I have a similar script that works fine...
6
by: Michiel Rapati-Kekkonen | last post by:
bsearch finds me only the first occurrence of something I'm looking for, but I would like to know the place in the list where it is found. The index of it's place in the array. So that I can check...
14
by: Roman Mashak | last post by:
Hello, All! Is there an easy way to determine that array e.g. int X contains ordered items (for example, ascending), except running loop with comparison of items? It would be good to provide...
4
by: Davy | last post by:
For example, I have a vector: double vector={1.11,2.38,4,53,17.14...,89.12,91.34} And if the Key I want is 5.2, the nearest item will be 4,53. I found that if the STEP of the vector is constant,...
0
by: jyotsnamk | last post by:
Hi, I am facing a problem using bsearch . Even though the data Im searching for is present in the array, the function returns a NULL object. The array is a structure with the following...
4
by: Steph | last post by:
Hello, I have filled a dynamic array of strings (realloc() + malloc()) char **sMyArray; At the end, I get correctly sMyArray = "STRING_0", sMyArray = "STRING_1", etc... But I'm unable to...
4
by: Amandil | last post by:
Hi, all. I'd like to check whether a certain string (one that I got from a user, or read from a file) is contained in a table of strings. The format of the table is char *table = { "string1",...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.