a small bsearch example

Discussion in 'C Programming' started by Ramprasad A Padmanabhan, Oct 28, 2003.

  1. 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,"END") != 0){
    char *tmp;
    tmp = bsearch(find, words, 4,4, cmp1);
    printf("Finding word %s\t==>\t Got %s\n",find,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)
     
    Ramprasad A Padmanabhan, Oct 28, 2003
    #1
    1. Advertising

  2. Ramprasad A Padmanabhan

    Mark Gordon Guest

    On Tue, 28 Oct 2003 13:37:57 +0530
    Ramprasad A Padmanabhan <> 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,"END") != 0){
    > char *tmp;


    char **tmp;

    Again, bsearch returns a pointer to the found element.

    > tmp = bsearch(find, words, 4,4, cmp1);
    > printf("Finding word %s\t==>\t Got %s\n",find,tmp);


    if (tmp)
    printf("Finding word %s\t==>\t Got %s\n",find,*tmp);
    else
    printf("Finding word %s\t==>\t Failed\n",find);

    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.
     
    Mark Gordon, Oct 28, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artie Gold

    Re: bsearch in C

    Artie Gold, Aug 7, 2003, in forum: C Programming
    Replies:
    0
    Views:
    417
    Artie Gold
    Aug 7, 2003
  2. Richard Heathfield

    Re: bsearch in C

    Richard Heathfield, Aug 7, 2003, in forum: C Programming
    Replies:
    0
    Views:
    414
    Richard Heathfield
    Aug 7, 2003
  3. Eric Sosman

    Re: bsearch in C

    Eric Sosman, Aug 7, 2003, in forum: C Programming
    Replies:
    9
    Views:
    525
    Richard Heathfield
    Aug 12, 2003
  4. Ramprasad A Padmanabhan

    bsearch script segfault

    Ramprasad A Padmanabhan, Dec 4, 2003, in forum: C Programming
    Replies:
    11
    Views:
    506
    Ramprasad A Padmanabhan
    Dec 5, 2003
  5. Angus Comber

    Struggling with bsearch - on a struct!

    Angus Comber, Feb 5, 2004, in forum: C Programming
    Replies:
    4
    Views:
    365
    Al Bowers
    Feb 6, 2004
Loading...

Share This Page