Lakshya Munjal
Lakshya Munjal

Reputation: 77

bsearch function and structure in C

I am using an array of structures to store the information of n students. This array of structures is first sorted using qsort() function and then a roll number is searched in the sorted array of structures using bsearch() function. What code should i write for comparator function of bsearch() ?

Source Code:

#include<stdio.h>
#define SIZE 15

// search for a structure in array of structures using qsort() and bsearch()

struct student{
    int id;
    char name[30];
}S[SIZE];

int compare(const void* S, const void* T){
    int id1 = ((struct student *)S) -> id;
    int id2 = ((struct student *)T) -> id;

    return id1 - id2;
}

struct student comapre1(const void* S, const void* T){
    // what code should i include here
}

void main(){
    int size, i;
    printf("How many students are there ?: ");
    scanf("%d", &size);
    printf("----------------------------------------------------------\n");

    for(i = 0 ; i < size ; i++){
        printf("Student %d\nEnter roll number: ",i+1);
        scanf("%d", &S[i].id);
        while(getchar() != '\n');
        printf("Enter name: ");
        gets(S[i].name);
        printf("----------------------------------------------------------\n");
    }

    qsort(S, SIZE, sizeof(struct student), compare);        // sorting array of structues

    int key;    // roll number to be searched

    printf("Enter roll number whose record wants to be searched: ");
    scanf("%d", &key);

    struct student *res = bsearch(&key, S, SIZE, sizeof(struct student), compare1);

    if(res != NULL){    
        // display name and id of record found
    }else
        printf("not found");
}

Upvotes: 0

Views: 1139

Answers (4)

chux
chux

Reputation: 153517

Code can use the same compare function as used in qsort() by first forming a struct student and use its .id member.

struct student dummy;
dummy.id = key;
struct student *res = bsearch(&dummy, S, SIZE, sizeof S[0], compare1);

Alternatively code could use a different compare and use the int key directly.

int bsearch_compare(const void* key_ptr, const void* element_ptr){
  int id1 = *((const int *)key_ptr);
  int id2 = ((const struct student *)element_ptr)->id;
  return id1 - id2;
}

struct student *res = bsearch(&key, S, SIZE, sizeof S[0], bsearch_compare);

For correct full range int functionality, change in both compare functions:

  // return id1 - id2;
  return (id1 > id2) - (id1 < id2);

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754060

Normally, you use the same function for sorting and searching — it guarantees that the search uses the same criteria as the sorting.

There are differences between bsearch() and qsort() in the way that arguments are passed to the comparator, and occasionally you can exploit the difference. You might not have a fully populated structure to pass as the key to the search, for example, but the fields used for sorting the data should be present in the key too.

Until you have a concrete use case that requires such shenanigans, use the same comparator for both sorting and searching.

Upvotes: 2

Afshin
Afshin

Reputation: 9173

Use same compare function for both bsearch() and qsort(), but remember that the key for bsearch() should be as struct student. So your code will be like this:

struct student student_key;    // roll number to be searched

printf("Enter roll number whose record wants to be searched: ");
scanf("%d", &(student_key.id));

struct student *res = (struct student *)bsearch(&student_key, S, size, sizeof(struct student), compare);

and final thing. Never use gets(). Always use fgets() at least.

Upvotes: 2

erik258
erik258

Reputation: 16304

The Man Page for bsearch provides a decent example. You must use the same comparison function for both qsort and bsearch, as bsearch assumes a sorted list and uses its comparison function to decide the direction of the next search in its iterative searching process.

At first glance, I don't think you need to redefine your comparison function. You should be able to use compare for both qsort and bsearch, and in fact, these functions must be equivalent if bsearch is going to successfully search what qsort ordered.

Upvotes: 1

Related Questions