Results 1 to 5 of 5

Thread: What is the bsearch() : C

  1. #1
    Join Date
    Jan 2010
    Posts
    22

    What is the bsearch() : C

    I has just started to learn the C programming language. I don't know anything about the C language but From when I started to learn I noticed that the C language is the basic of all programming languages. I heard about the library functions but I don't what are they and what they can do. So, I want know about the bsearch() function of the C language. I also want to know what is the purpose of the bsearch() function and how can I use the bsearch() function in the Coding.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    bsearch() : C

    According to my knowledge bsearch() function performs a binary search through the elements of a table. This bsearch() function can uses your compar() function to compare an elements in the table.You have to noticed that the compar() function you use could not do a byte-by-byte comparison between the table elements and the key. The following is the syntax of the bsearch() function :
    #include <stdlib.h>
    pt = bsearch( ky, tbl, tbsiz, kysz, cmpr);

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    What is the bsearch()

    The bsearch() function can performs a binary search of a num elements of sorted array that matches the object pointed to by the key for an item. Each an array element is width bytes in size. The compar is the comparison function pointed can be called with two an arguments that can points to an array elements. This compar function must return an integer equal to, less than or greater than zero if the key object is equal to, less than or greater than the array element respectively.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    What is the bsearch()

    Example of bsearch() function :
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    static const char *kywrds[] = {"auto", "break", "case", "char", "while"};
    #define NUM_KW sizeof(kywrds) / sizeof(char *)
    int kw_compare( const void *p3, const void *p4 )
    {
    const char *p3c = (const char *) p3;
    const char **p4c = (const char **) p4;
    return( strcmp( p3c, *p4c ) );
    }
    int kywrd_lookup( const char *nm )
    {
    const char **ky;
    ky = (char const **) bsearch( nm, kywrds,
    NUM_KW, sizeof( char * ), kw_compare );
    if( ky == NULL ) return( -1 );
    return ky - kywrds;
    }
    void main()
    {
    printf( "%d\n", kywrd_lookup( "case" ) );
    printf( "%d\n", kywrd_lookup( "crigger" ) );
    printf( "%d\n", kywrd_lookup( "auto" ) );
    }

    Output of the above program is as follows :
    2
    -1
    0

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    What is the bsearch() : C

    One more example on bsearch() function :
    #include "config.h"
    #include "ansidecl.h"
    #include <sys/types.h>
    #include <stdio.h>
    void *
    bsearch(key, base0, nmemb, size, compar)
    register void *key;
    void *base0;
    size_t nmemb;
    register size_t size;
    register int (*compar)();
    {
    register char *base = base0;
    register int lim, cmp;
    register void *p;
    for (lim = nmemb; lim != 0; lim >>= 1)
    {
    p = base + (lim >> 1) * size;
    cmp = (*compar)(key, p);
    if (cmp == 0)
    return (p);
    if (cmp > 0)
    {
    base = (char *)p + size;
    lim--;
    }
    }
    return (NULL);
    }

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,556,361.06359 seconds with 16 queries