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

a small bsearch example

P: n/a
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
Ram
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>

int cmp1(const void *p1, const void *p2)
{
char *s = p1;
char *target = p2;
printf("Comparing %s ==> %s \n",s,target);
return(strcmp(s,target));
}

int main(int argc,char* argv[]){
int i=0;
char* find[] = {"a.c","b.c","c.c","END"};
char* words[] ={"a.c","b.c","c.c","zzz"};

while(strcmp(find[i],"END") != 0){
char *tmp;
tmp = bsearch(find[i], words, 4,4, cmp1);
printf("Finding word %s\t==>\t Got %s\n",find[i],tmp);
i++;
}
return(0);
}

My output looks like this
Finding word a.c ==> Got (null)
Finding word b.c ==> Got (null)
Finding word c.c ==> Got (null)

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Tue, 28 Oct 2003 13:37:57 +0530
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote:
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
Ram
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
You don't need all of these headers, although including a standard
header you don't need is not going to break any otherwise well written
code.
int cmp1(const void *p1, const void *p2)
{
char *s = p1;
char *target = p2;
char *target = *(char**)p2;

Each of your elements in words is a pointer to a char* and bsearch
passes you a pointer to the element, so you are getting a pointer to a
pointer to char which you want to dereference.
printf("Comparing %s ==> %s \n",s,target);
return(strcmp(s,target));
}

int main(int argc,char* argv[]){
int i=0;
char* find[] = {"a.c","b.c","c.c","END"};
char* words[] ={"a.c","b.c","c.c","zzz"};

while(strcmp(find[i],"END") != 0){
char *tmp;
char **tmp;

Again, bsearch returns a pointer to the found element.
tmp = bsearch(find[i], words, 4,4, cmp1);
printf("Finding word %s\t==>\t Got %s\n",find[i],tmp);
if (tmp)
printf("Finding word %s\t==>\t Got %s\n",find[i],*tmp);
else
printf("Finding word %s\t==>\t Failed\n",find[i]);

Otherwise if the find fails any you get back a null pointer you are
invoking undefined behaviour and anything could happen. You are just
lucky that your implementation decided to print (null) rather than
causing a small thermonuclear explosion.
i++;
}
return(0);
}

--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.