Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a small bsearch example

Reply
Thread Tools

a small bsearch example

 
 
Ramprasad A Padmanabhan
Guest
Posts: n/a
 
      10-28-2003
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)

 
Reply With Quote
 
 
 
 
Mark Gordon
Guest
Posts: n/a
 
      10-28-2003
On Tue, 28 Oct 2003 13:37:57 +0530
Ramprasad A Padmanabhan <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struggling with bsearch - on a struct! Angus Comber C Programming 4 02-06-2004 04:49 AM
bsearch script segfault Ramprasad A Padmanabhan C Programming 11 12-05-2003 01:00 PM
Re: bsearch in C Eric Sosman C Programming 9 08-12-2003 05:21 AM
Re: bsearch in C Richard Heathfield C Programming 0 08-07-2003 09:12 PM
Re: bsearch in C Artie Gold C Programming 0 08-07-2003 09:06 PM



Advertisments