Hello everyone
I am using bsearch() method to find whether the element is in list or not, but i don't know how to find the index of that element.
here is the code which i have done:--

#include <stdlib.h>
#include <stdio.h>
int arr[11] = { 10,22,37,49,51,66,68,73,75,79,81 };
int comp(const void *a, const void *b);
int main(void) {
        int ch;
        int *p;
        printf("Enter a number: ");
        scanf("%d",&ch);
        p = (int *) bsearch(&ch, arr,11,sizeof(int), comp);
        if (p)
           printf(" %d is in the Array\n", *p);
        else
           printf(" %d is not in the Array\n",ch);
        return 0;
        }
int comp (const void * a, const void * b)
{
       return ( *(int*)a - *(int*)b );
}

You should be able to subtract the base address of the array from the return value from bsearch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.